简体   繁体   中英

Permutation of lists of list python

I want all the permutation and combinations of lists of lists that i have, i am using itertools.product to calculate the permutation but my computer hangs for indefinite time, What i might be doing wrong?

import itertools

#Lists of all the possible dimensions
upperchest_dim=range(32,52,1)
upperback_dim=range(32,52,1)
chest_dim=range(32,52,1)
waist_dim=range(32,52,1)
hip_dim=range(32,52,1)
bicep_dim=range(32,52,1)
elbow_dim=range(32,52,1)
thigh_dim=range(32,52,1)
knee_dim=range(32,52,1)
calf_dim=range(32,52,1)
height_dim=range(32,52,1)

#List of lists total
dimensions=[upperchest_dim,upperback_dim,chest_dim,waist_dim,hip_dim,bicep_dim,elbow_dim,thigh_dim,knee_dim,calf_dim,height_dim]

#Generate permutations of all the dimensions
print list(itertools.product(*dimensions))

The list(itertools.product(*dimensions)) should have all the unique permutations possible for all the dimensions.

-- EDIT: I think i am doing something wrong. I want a list of lists which has all the unique dimensions, for example [32,33,34,45,34,23,42,43,43,45,33] This is one dimension and the result should not contain this exact list again as this represents one body type.

That list would have 20 ** 11 = 2 ** 11 * 10 ** 11 = 204800000000000 elements. That's what's wrong.

Although itertools.product is an iterator that wouldn't hang indefinitely (it'd just take extremely long to iterate over all of it), turning it into a list() will hang until it has used all memory.

No need for all this stuff, you can just use permutations :

from itertools import permutations

var = permutations([12, 34, 123, 12, 31, 231])

for perm in var:
    print perm

Even works with a list of lists:

from itertools import permutations

var = permutations([[1, 2, 3, 4], [24, 5, 12, 3], 123, 12, 31, 231])

for perm in var:
    print perm

Working example .

If for some reason you want all possible permutations, even of permutations of the lists within the list, then, you will have to use the following code:

from itertools import permutations

var = [[1, 2, 3, 4], [24, 5, 12, 3], 123, 12, 31, 231]

# Getting all permutations of lists within the lists
output = []
for l in var:
    if isinstance(l, list):
        output += permutations(l)
    else:
        output.append(l)

perm = permutations(output)

for p in perm:
    print p

Working example .

If you on the last line do

for d in itertools.product(*dimensions): print(d)

it start to print

... (32, 32, 32, 32, 32, 32, 32, 33, 37, 47, 45) (32, 32, 32, 32, 32, 32, 32, 33, 37, 47, 46) (32, 32, 32, 32, 32, 32, 32, 33, 37, 47, 47) (32, 32, 32, 32, 32, 32, 32, 33, 37, 47, 48) ...

So "nothing" is wrong, the resulting list is so just enormously big that it can't be computed all once

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM