about list

rurpy at yahoo.com rurpy at yahoo.com
Sun Nov 20 22:05:01 EST 2005


Shi Mu wrote:
> How to run a function to make [1,2,4] become [[1,2],1,4],[2,4]]?
> Thanks!

You want [[1,2],[1,4],[2,4]]?  That is, all combinations of 2 items
from
the list?  You might want to look at:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465

>>> import * from xpermutations
>>> [x for x in UniqueCombinations ([1,2,4], 2)]
[[1, 2], [1, 4], [2, 4]]

That web page also gives urls to other recipies that do the same thing.




More information about the Python-list mailing list