-1

I have an array ['cat','dog','rabbit','snake','sheep','cow','horse','pig'] and I want to output every combination of the elements, while also removing elements until the the data set includes one of each animal.

['cat','dog','rabbit']
['cat','dog',]
['cat']
['dog','rabbit']
['dog']
['cat','rabbit']
['rabbit']

but with a lot more data etc...

Are there any modules to help with this?

Thanls

mbudge
  • 557
  • 13
  • 28

1 Answers1

0

Try itertools.combinations from the standard library to combine the elements.

This module contains a lot of cool stuff for the kind of job you want to get done. The great part is that every function on this module, returns an iterable instead of a list, so you can use it to create "on-demand" results, as combinatorial algorithms on large sets of data can be really expensive.

avenet
  • 2,894
  • 1
  • 19
  • 26