Drumma0 - 1 year ago 40

Python Question

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):

x[n]=i

if n==0:

print x

else:

XUniv(q,x,n-1)

Answer

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]]
```