Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to create lists of n-tuples efficiently?

The ultimate problem I'm trying to solve, is that given two integers p and q, I would like to create a list (say "List1"), whose entries are (all of the) lists of length 2q which:

1) For all L in List1, the first q entries are between 1 and pq (inclusive), and the next q entries are between pq+1 and 2pq (inclusive)

2) For all L in List1, the entries are strictly increasing, that is for all 1<=i<=2pq, L[i] < L[i+1]

I have a way of doing this which is very "brute force", where I first construct the list of lists of length 1, then the list of lists of length 2, etc.
I'm imagining (hoping) there exists some way for me to just iterate over all 2q -tuples (without repetition) of numbers from 1 to 2pq Something along the lines of

List1 = []
for x in 2q-tuples of [1, 2, ..., 2pq]:
    if x satisfies 1) and 2):
        List1.append(x)

I suppose I would want to convert x to a list before appending to List1, or many there's some way to iterate over lists instead of tuples. That's not so important, I just want an efficient way to construct these lists.

How to create lists of n-tuples efficiently?

The ultimate problem I'm trying to solve, is that given two integers p and q, I would like to create a list (say "List1"), whose entries are (all of the) lists of length 2q which:

1) For all L in List1, the first q entries are between 1 and pq0 and pq-1 (inclusive), and the next q entries are between pq+1 and 2pqpq and 2pq-1 (inclusive)

2) For all L in List1, the entries are strictly increasing, that is for all 1<=i<=2pq, L[i] < L[i+1]

I have a way of doing this which is very "brute force", where I first construct the list of lists of length 1, then the list of lists of length 2, etc.
I'm imagining (hoping) there exists some way for me to just iterate over all 2q -tuples (without repetition) of numbers from 1 to 2pq Something along the lines of

List1 = []
for x in 2q-tuples of [1, [0, 1, 2, ..., 2pq]:
2pq-1]:
    if x satisfies 1) and 2):
        List1.append(x)

I suppose I would want to convert x to a list before appending to List1, or many there's some way to iterate over lists instead of tuples. That's not so important, I just want an efficient way to construct these lists. lists.