Thursday, May 19, 2011

Creating Combinations

You should have python2.6 installed for this to work.

>>> import itertools
>>> lx = ['none','P1','P2','P3']
>>> lx
['none', 'P1', 'P2', 'P3']
>>> l2 = itertools.combinations(lx,2) # combination of 2 objects
>>> for x in l2:
...     if 'P1' in x:
...             p1.append(list(x))
...
>>> p1
[['none', 'P1'], ['P1', 'P2'], ['P1', 'P3']]

The above example created a list that contains 2 combinations that has 'P1' in it.

No comments:

Post a Comment