A Python Recipe for Generating Subsets
I recently found myself needing to generate subsets of collections. In one situation I needed to generate all combinations of possibly…
I recently found myself needing to generate subsets of collections. In one situation I needed to generate all combinations of possibly interacting diseases (up to 6 diseases from a set of ~24 diseases) to diagnose a given set of symptoms. And in another, I needed to generate combinations of insufficient input data to assert that a system under test would report an error given the insufficient data.
At first, I didn’t see these problems as set problems. I saw them exclusively as combination problems, but the set perspective turned out to be the right lens through which to view them.
The recipe in this article is a succinct procedure for generating subsets that resulted from my mulling over the aforementioned problems and enlisting the help of some Internet friends to put a name to some code I had written — a power set-producing procedure, which I later generalized to what is shown below.
The recipe
Articles you should watch next
- Set Basics in Python
- Data Structures, Stacks, and Queues in Python
- Demystifying __init__(self, …) in Python
More Computing resources
Watch videos covering a variety of topics in Computing at OnelTalksTech.com