[英]Permutations in list of lists
So, say I have a list of lists like 所以说我有一个清单清单
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
How do I get all possible permutations with the restriction that I can only pick 1 item per list? 如何在每个列表只能选择1个项目的限制下获得所有可能的排列? Meaning that 147 or 269 would be possible permutations, whereas 145 would be wrong since 4 and 5 are in the same list.
意味着147或269是可能的排列,而145将是错误的,因为4和5在同一列表中。 Also, how does this work for a list containing any number of lists?
此外,这对于包含任意数量列表的列表如何起作用?
This works for me in Python 2.7 and 3.5 这对我适用于Python 2.7和3.5
import itertools
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list(itertools.product(*l)))
it 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)]
this worked in python 3, see the comment on the last line for python 2 这在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)
I wouldn't call what you are looking for permutations, but the following recursive algorithm should return what I assume you would like to see 我不会称呼您正在寻找的排列,但是以下递归算法应该返回我认为您想看到的内容
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]])
My output was the following 27 items which could later be converted back to integers if you like; 我的输出是以下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'] [“ 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”]
You could use recursion. 您可以使用递归。
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)
You can use itertools for this! 您可以为此使用itertools !
from itertools import product
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list(product(*l)))
A few things to note: 注意事项:
I'm passing *l
instead of simply l
as product
expects iterables as arguments, not a list of iterables; 我传递
*l
,而不是简单的l
作为product
预计iterables作为参数,而不是一个iterables的列表; another way to write this would have been: 编写此内容的另一种方式是:
product([1, 2, 3], [4, 5, 6], [7, 8, 9])
ie, passing every list as a single argument. 即,将每个列表作为单个参数传递。
*l
unpacks the elements of l
into arguments for you. *l
为您将l
的元素解压缩为参数。
product
does not return a list, but a generator. product
不返回列表,而是生成器。 You can pass that to anything that expects an iterable. 您可以将其传递给任何期望可迭代的对象。 "Printing" the generator would not be helpful (you would not see the content of the resulting list, but
<itertools.product object...>
which is only mildly interesting); “打印”生成器将无济于事(您不会看到结果列表的内容,但是
<itertools.product object...>
只是有点有趣); this is why I'm forcing a conversion to a list using list()
这就是为什么我使用
list()
强制转换为列表的原因
print()
with parentheses allows this code to be compatible with Python 2 & 3. print()
可使此代码与Python 2和3兼容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.