列表列表中的排列
Permutations in list of lists
所以,假设我有一个列表列表,例如
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
如何在每个列表只能选择 1 项的限制下获得所有可能的排列?
这意味着 147 或 269 是可能的排列,而 145 是错误的,因为 4 和 5 在同一个列表中。
另外,这对于包含任意数量列表的列表如何工作?
这在 python 3 中有效,请参阅 python 2
最后一行的注释
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
row1, row2, row3 = l
# get all the permutations as lists [1,4,7], etc.
permutations = ([x, y, z] for x in row1 for y in row2 for z in row3)
# get strings to have a more easily readable output
permutation_strings = (''.join(map(str, permutation))
for permutation in permutations)
print(*permutation_strings)
# in python2 you can use: print list(permutation_strings)
这适用于 Python 2.7 和 3.5
import itertools
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list(itertools.product(*l)))
它returns
[(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9)]
我不会调用你正在寻找的排列,但下面的递归算法应该return我假设你想看到的
def get_all_possibilities(S, P=[]):
if S == []:
return P
s = S[0]
if P == []:
for x in s:
P.append(str(x))
return get_all_possibilities(S[1:], P)
else:
new_P = []
for x in s:
for p in P:
new_P.append(p + str(x))
return get_all_possibilities(S[1:], new_P)
print get_all_possibilities([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
我的输出是以下 27 项,如果您愿意,可以稍后将其转换回整数;
['147', '247', '347', '157', '257', '357', '167', '267', '367', '148', '248', '348', '158', '258', '358', '168', '268', '368', '149', '249', '349', '159', '259', '359 ', '169', '269', '369']
你可以使用递归。
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
def permutate(w, l):
for x in l[0]:
if len(l) > 1:
permutate(w + str(x), l[1:])
else:
print w + str(x)
permutate("", l)
您可以为此使用 itertools!
from itertools import product
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list(product(*l)))
注意几点:
我传递的是 *l
而不是简单的 l
因为 product
期望可迭代作为参数,而不是可迭代列表;另一种写法是:
product([1, 2, 3], [4, 5, 6], [7, 8, 9])
即,将每个列表作为单个参数传递。 *l
将 l
的元素解压缩为参数。
product
不是 return 列表,而是生成器。您可以将其传递给任何需要迭代的东西。 "Printing" 生成器不会有帮助(您不会看到结果列表的内容,但 <itertools.product object...>
只是有点意思);这就是为什么我使用 list()
强制转换为列表的原因
- 使用带括号的
print()
允许此代码与 Python 2 & 3 兼容。
所以,假设我有一个列表列表,例如
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
如何在每个列表只能选择 1 项的限制下获得所有可能的排列? 这意味着 147 或 269 是可能的排列,而 145 是错误的,因为 4 和 5 在同一个列表中。 另外,这对于包含任意数量列表的列表如何工作?
这在 python 3 中有效,请参阅 python 2
最后一行的注释l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
row1, row2, row3 = l
# get all the permutations as lists [1,4,7], etc.
permutations = ([x, y, z] for x in row1 for y in row2 for z in row3)
# get strings to have a more easily readable output
permutation_strings = (''.join(map(str, permutation))
for permutation in permutations)
print(*permutation_strings)
# in python2 you can use: print list(permutation_strings)
这适用于 Python 2.7 和 3.5
import itertools
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list(itertools.product(*l)))
它returns
[(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9)]
我不会调用你正在寻找的排列,但下面的递归算法应该return我假设你想看到的
def get_all_possibilities(S, P=[]):
if S == []:
return P
s = S[0]
if P == []:
for x in s:
P.append(str(x))
return get_all_possibilities(S[1:], P)
else:
new_P = []
for x in s:
for p in P:
new_P.append(p + str(x))
return get_all_possibilities(S[1:], new_P)
print get_all_possibilities([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
我的输出是以下 27 项,如果您愿意,可以稍后将其转换回整数;
['147', '247', '347', '157', '257', '357', '167', '267', '367', '148', '248', '348', '158', '258', '358', '168', '268', '368', '149', '249', '349', '159', '259', '359 ', '169', '269', '369']
你可以使用递归。
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
def permutate(w, l):
for x in l[0]:
if len(l) > 1:
permutate(w + str(x), l[1:])
else:
print w + str(x)
permutate("", l)
您可以为此使用 itertools!
from itertools import product
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list(product(*l)))
注意几点:
我传递的是
*l
而不是简单的l
因为product
期望可迭代作为参数,而不是可迭代列表;另一种写法是:product([1, 2, 3], [4, 5, 6], [7, 8, 9])
即,将每个列表作为单个参数传递。
*l
将l
的元素解压缩为参数。product
不是 return 列表,而是生成器。您可以将其传递给任何需要迭代的东西。 "Printing" 生成器不会有帮助(您不会看到结果列表的内容,但<itertools.product object...>
只是有点意思);这就是为什么我使用list()
强制转换为列表的原因
- 使用带括号的
print()
允许此代码与 Python 2 & 3 兼容。