First time here? Check out the FAQ!

Ask Your Question
1

iterating over a combinatorial class

asked 13 years ago

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 13 years ago

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!

Preview: (hide)
link

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 ( 13 years ago )

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: 13 years ago

Seen: 434 times

Last updated: Jun 02 '11