Ask Your Question
1

iterating over a combinatorial class

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

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 = Q.next()
    for i in range(prof[j]):
        L.append(tp)

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
1

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

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
True

functional 4tw!

edit flag offensive delete link more

Comments

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 +0100 )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

Stats

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

Seen: 414 times

Last updated: Jun 02 '11