Ask Your Question

iterating over a combinatorial class

asked 2011-06-02 12:35:37 +0200

kcrisman gravatar image

It is somewhat annoying that len and next don't do what you think with combinatorial classes. Is there any more Pythonic (e.g. list comprehension) way to do the following?

prof = [1,3,2,0,0,3]
L = []
P = Permutations(3)
Q = P.__iter__()
for j in range(P.cardinality()):
    tp =
    for i in range(prof[j]):

Part of the issue is that I would really rather not use Q, since it is "used up" when I'm done. I really want something that is succinct, but I just could not find a way to do it.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2011-06-02 13:07:42 +0200

DSM gravatar image
sage: L2a = flatten([[perm]*count for perm, count in zip(Permutations(3), prof)])
sage: L2b = sum(([perm]*count for perm, count in zip(Permutations(3), prof)), [])
sage: L == L2a == L2b

functional 4tw!

edit flag offensive delete link more


I see what my problem was - I used * with list(p) for each permutation, and tried to use list comprehension without that, but didn't consider not making a list in the first place with the comprehension.

kcrisman gravatar imagekcrisman ( 2011-06-02 13:47:42 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-06-02 12:35:37 +0200

Seen: 328 times

Last updated: Jun 02 '11