是否存在排列列表列表的pythonic方式?

时间:2016-09-28 10:59:56

标签: python list python-2.7 permutation

我有一个包含唯一字符串的列表列表,我希望生成任意数量的不同排序方式。该列表可能如下所示:

list = [[a], [b,c], [d], [e,f,g]]

列表的顺序必须相同,但我想在列表中随机排序,然后将它们放在一个列表中,例如

list1 = [a,b,c,d,e,f,g]
list2 = [a,c,b,d,f,e,g]
...
...
listN = [a,c,b,d,f,g,e]

实现这一目标的好方法是什么?我在python 2.7上。

5 个答案:

答案 0 :(得分:3)

您可以通过获取子列表的排列的笛卡尔积,然后展平生成的嵌套元组来实现此目的。

from itertools import permutations, product, chain

lst = [['a'], ['b', 'c'], ['d'], ['e', 'f', 'g']]

for t in product(*[permutations(u) for u in lst]):
    print([*chain.from_iterable(t)])

<强>输出

['a', 'b', 'c', 'd', 'e', 'f', 'g']
['a', 'b', 'c', 'd', 'e', 'g', 'f']
['a', 'b', 'c', 'd', 'f', 'e', 'g']
['a', 'b', 'c', 'd', 'f', 'g', 'e']
['a', 'b', 'c', 'd', 'g', 'e', 'f']
['a', 'b', 'c', 'd', 'g', 'f', 'e']
['a', 'c', 'b', 'd', 'e', 'f', 'g']
['a', 'c', 'b', 'd', 'e', 'g', 'f']
['a', 'c', 'b', 'd', 'f', 'e', 'g']
['a', 'c', 'b', 'd', 'f', 'g', 'e']
['a', 'c', 'b', 'd', 'g', 'e', 'f']
['a', 'c', 'b', 'd', 'g', 'f', 'e']

如果您需要在Python 2中执行此操作,则可以使用以下命令替换打印行:

print list(chain.from_iterable(t))

这是一个更紧凑的版本,灵感来自ewcz的答案:

for t in product(*map(permutations, lst)):
    print list(chain.from_iterable(t))

答案 1 :(得分:2)

from itertools import permutations, product

L = [['a'], ['b','c'], ['d'], ['e', 'f', 'g']]

for l in product(*map(lambda l: permutations(l), L)):
    print([item for s in l for item in s])

产生

['a', 'b', 'c', 'd', 'e', 'f', 'g']
['a', 'b', 'c', 'd', 'e', 'g', 'f']
['a', 'b', 'c', 'd', 'f', 'e', 'g']
['a', 'b', 'c', 'd', 'f', 'g', 'e']
['a', 'b', 'c', 'd', 'g', 'e', 'f']
['a', 'b', 'c', 'd', 'g', 'f', 'e']
['a', 'c', 'b', 'd', 'e', 'f', 'g']
['a', 'c', 'b', 'd', 'e', 'g', 'f']
['a', 'c', 'b', 'd', 'f', 'e', 'g']
['a', 'c', 'b', 'd', 'f', 'g', 'e']
['a', 'c', 'b', 'd', 'g', 'e', 'f']
['a', 'c', 'b', 'd', 'g', 'f', 'e']

答案 2 :(得分:0)

这可能不是最优雅的解决方案,但我认为它可以满足您的需求

from itertools import permutations
import numpy as np

def fac(n):
    if n<=1:
        return 1
    else:
        return n * fac(n-1)

lists = [['a'], ['b','c'], ['d'], ['e','f','g']]

combined = [[]]
for perm in [permutations(l,r=len(l)) for l in lists]:
    expanded = []
    for e in list(perm):
        expanded += [list(l) + list(e) for l in combined]
    combined = expanded

## check length

print np.prod(map(fac,map(len,lists))), len(combined)

print '\n'.join(map(str,combined))

答案 3 :(得分:-1)

您可以展平列表,然后只需生成其排列:

from itertools import chain, permutations

li = [['a'], ['b','c'], ['d'], ['e','f','g']]
flattened = list(chain.from_iterable(li))
for perm in permutations(flattened, r=len(flattened)):
     print(perm)
>> ('a', 'b', 'c', 'd', 'e', 'f', 'g')
   ('a', 'b', 'c', 'd', 'e', 'g', 'f')
   ('a', 'b', 'c', 'd', 'f', 'e', 'g')
   ('a', 'b', 'c', 'd', 'f', 'g', 'e')
   ('a', 'b', 'c', 'd', 'g', 'e', 'f')
   ('a', 'b', 'c', 'd', 'g', 'f', 'e')
   ('a', 'b', 'c', 'e', 'd', 'f', 'g')
   ('a', 'b', 'c', 'e', 'd', 'g', 'f')
   ('a', 'b', 'c', 'e', 'f', 'd', 'g')
   ...
   ...
   ...

答案 4 :(得分:-1)

from itertools import chain, permutations

your_list = [[a], [b,c], [d], [e,f,g]]
flattened = chain.from_iterable(your_list)
perms = permutations(flattened)
for perm in perms:
    print perm

参考文献: