xpermutations (GitHub) is a library for making permutations or combinations.
>>> from xpermutations import xcombinations, xuniqueCombinations, xselections, xpermutations >>> from itertools import combinations, combinations_with_replacement, permutations, product >>> " ".join("".join(x) for x in xcombinations("1234", 2)) '12 13 14 21 23 24 31 32 34 41 42 43' >>> >>> " ".join("".join(x) for x in xuniqueCombinations("1234", 2)) '12 13 14 23 24 34' >>> " ".join("".join(x) for x in combinations("1234", 2)) '12 13 14 23 24 34' >>> >>> " ".join("".join(x) for x in xselections("1234", 2)) '11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44' >>> " ".join("".join(x) for x in product("1234", repeat=2)) '11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44' >>> >>> " ".join("".join(x) for x in xpermutations("123")) '123 132 213 231 312 321' >>> " ".join("".join(x) for x in permutations("123")) '123 132 213 231 312 321'
It’s based on a ActiveState recipe from 2003 by Ulrich Hoffmann, most of these can be easily done by the itertools, which was added in Python 2.3. The recipe “uses Python 2.2 generators to create appropriate generator objects, that can be use for example as ranges in for loops.” I am guessing itertools might have used code from the recipe.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.