Drumma0 Drumma0 - 1 year ago 63
Python Question

Creating all lists of length "n" from a list of q elements

For a couple of days I've been trying to find a way to create all lists of length "n" from a list of "q" elements. So far I've been able to get sage to print all of the lists I'm looking to obtain. However, since I need these lists for another program I need my program to output a list of all of these lists instead of just having Sage print the lists. What I have so far is:

def XUniv(q,x,n):#p is your alphabet size. x is a zero vector of any length. n is len(x)-1#
for i in range(q):
if n==0:
print x

Answer Source

Is something like this what you want?

sage: q=1
sage: n=3
sage: L = IntegerListsLex(min_part=0, max_part=q, length=n)
sage: list(L)
[[1, 1, 1],
 [1, 1, 0],
 [1, 0, 1],
 [1, 0, 0],
 [0, 1, 1],
 [0, 1, 0],
 [0, 0, 1],
 [0, 0, 0]]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download