Python 列表列表中的排列
那么,假设我有一个列表,比如Python 列表列表中的排列,python,list,permutation,Python,List,Permutation,那么,假设我有一个列表,比如 l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 如何在每个列表只能选择一个项目的限制下获得所有可能的排列? 这意味着147或269可能是排列,而145可能是错误的,因为4和5在同一个列表中。 另外,对于包含任意数量列表的列表,这是如何工作的?这在Python3中工作,请参见Python2最后一行的注释 l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] row1, row2, row3 = l # get al
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
如何在每个列表只能选择一个项目的限制下获得所有可能的排列?
这意味着147或269可能是排列,而145可能是错误的,因为4和5在同一个列表中。
另外,对于包含任意数量列表的列表,这是如何工作的?这在Python3中工作,请参见Python2最后一行的注释
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)
这在Python3中起作用,请参见Python2最后一行的注释
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)))
它回来了
[(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)]
这在Python 2.7和3.5中适用
import itertools
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list(itertools.product(*l)))
它回来了
[(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)]
我不会调用您正在寻找的置换,但是下面的递归算法应该返回我假设您希望看到的结果
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',268',368',149',249',349',159',259',359',169',269',369',我不会调用您正在寻找的置换,但是下面的递归算法应该返回我假设您希望看到的结果
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)
可以使用递归
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)
你可以用这个
from itertools import product
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list(product(*l)))
需要注意的几点:
- 我传递的是
,而不是简单的*l
,因为l
希望将iterables作为参数,而不是iterables列表;写这篇文章的另一种方式是:product
i、 例如,将每个列表作为单个参数传递product([1, 2, 3], [4, 5, 6], [7, 8, 9])
将*l
的元素解压缩为参数l
不返回列表,而是返回生成器。您可以将其传递给任何需要iterable的对象。“打印”生成器将没有帮助(您将看不到结果列表的内容,但产品
只是稍微有趣);这就是为什么我要使用list()
- 使用带有括号的
可以使此代码与Python 2和3兼容print()
- 您可以使用它
from itertools import product
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list(product(*l)))
需要注意的几点:
- 我传递的是
,而不是简单的*l
,因为l
希望将iterables作为参数,而不是iterables列表;写这篇文章的另一种方式是:product
i、 例如,将每个列表作为单个参数传递product([1, 2, 3], [4, 5, 6], [7, 8, 9])
将*l
的元素解压缩为参数l
不返回列表,而是返回生成器。您可以将其传递给任何需要iterable的对象。“打印”生成器将没有帮助(您将看不到结果列表的内容,但产品
只是稍微有趣);这就是为什么我要使用list()
- 使用带有括号的
可以使此代码与Python 2和3兼容print()
itertools
吗?你试过itertools
吗?这比我的好多了!这比我的好多了!