Python 获取所有可能的列表组合并将其保存在内存中
我需要以下问题的帮助: 我有一个列表,我想得到该列表的所有可能组合(但不要混淆子列表项的顺序,也不要混淆一个子列表项与另一个子列表项)。这是我到目前为止的代码:Python 获取所有可能的列表组合并将其保存在内存中,python,python-3.x,list,permutation,itertools,Python,Python 3.x,List,Permutation,Itertools,我需要以下问题的帮助: 我有一个列表,我想得到该列表的所有可能组合(但不要混淆子列表项的顺序,也不要混淆一个子列表项与另一个子列表项)。这是我到目前为止的代码: my_list = [['x', 'y', 'z'], ['z', 't', 'z'], ['k', 't', 'x']] print("my list is", my_list) for x in itertools.permutations(my_list): y = list(x) print(
my_list = [['x', 'y', 'z'], ['z', 't', 'z'], ['k', 't', 'x']]
print("my list is", my_list)
for x in itertools.permutations(my_list):
y = list(x)
print(y)
print("out of the loop y is", y)
输出:
my list is [['x', 'y', 'z'], ['z', 't', 'z'], ['k', 't', 'x']]
y is [['x', 'y', 'z'], ['z', 't', 'z'], ['k', 't', 'x']]
y is [['x', 'y', 'z'], ['k', 't', 'x'], ['z', 't', 'z']]
y is [['z', 't', 'z'], ['x', 'y', 'z'], ['k', 't', 'x']]
y is [['z', 't', 'z'], ['k', 't', 'x'], ['x', 'y', 'z']]
y is [['k', 't', 'x'], ['x', 'y', 'z'], ['z', 't', 'z']]
y is [['k', 't', 'x'], ['z', 't', 'z'], ['x', 'y', 'z']]
out of the loop y is [['k', 't', 'x'], ['z', 't', 'z'], ['x', 'y', 'z']]
如您所见,如果我在循环中打印y
,我会进行我想要的组合,但由于循环外也有意义,所以只有最后一个列表保留在内存中,用于y
如何将所有组合保存在内存中
代码的预期目标是稍后生成一个新列表,它需要检查新列表是否是y
的组合之一。因此:
...
new_list = [['z', 't', 'z'], ['x', 'y', 'z'], ['k', 't', 'x']]
if new_list in y:
print("Yeah! new_list is one of the combinations of my_list")
else:
print("Oops! Try again!")
谢谢 您可能希望将
y
声明为列表,并将新列表附加到该列表中:
y=[]
my_list=['x','y','z'],['z','t','z'],['k','t','x']
打印(“我的列表是”,我的列表)
对于itertools.排列中的x(我的列表):
y、 附加(列表(x))
打印(y)
打印(“循环外y为”,y)
现在来看最后一段:
new_list = [['z', 't', 'z'], ['x', 'y', 'z'], ['k', 't', 'x']]
if new_list in y:
print("Yeah! new_list is one of the combinations of my_list")
else:
print("Oops! Try again!")
你会得到:
Yeah! new_list is one of the combinations of my_list
如果您不使用循环,而是使用其他答案中所示的列表,那么您的代码甚至可以更加python
您可以替换:
for x in itertools.permutations(my_list):
y.append(list(x))
print(y)
与:
并得到相同的结果。您只需将生成器转换为列表:
list(itertools.permutations(my_list))
您可以更高效地执行此操作,而无需在几行代码中对append()
进行不必要的调用
import itertools
my_list = [['x', 'y', 'z'], ['z', 't', 'z'], ['k', 't', 'x']]
y = [list(x) for x in itertools.permutations(my_list)]
print("out of the loop y is", y)
new_list = [['z', 't', 'z'], ['x', 'y', 'z'], ['k', 't', 'x']]
if new_list in y:
print("Yeah! new_list is one of the permutations of my_list")
else:
print("Oops! Try again!")
代码输出:
out of the loop y is [[['x', 'y', 'z'], ['z', 't', 'z'], ['k', 't', 'x']], [['x', 'y', 'z'], ['k', 't', 'x'], ['z', 't', 'z']], [['z', 't', 'z'], ['x', 'y', 'z'], ['k', 't', 'x']], [['z', 't', 'z'], ['k', 't', 'x'], ['x', 'y', 'z']], [['k', 't', 'x'], ['x', 'y', 'z'], ['z', 't', 'z']], [['k', 't', 'x'], ['z', 't', 'z'], ['x', 'y', 'z']]]
Yeah! new_list is one of the permutations of my_list
您可以按如下方式在中构建排列列表:
>>> [list(p) for p in itertools.permutations(my_list)]
[[['x', 'y', 'z'], ['z', 't', 'z'], ['k', 't', 'x']],
[['x', 'y', 'z'], ['k', 't', 'x'], ['z', 't', 'z']],
[['z', 't', 'z'], ['x', 'y', 'z'], ['k', 't', 'x']],
[['z', 't', 'z'], ['k', 't', 'x'], ['x', 'y', 'z']],
[['k', 't', 'x'], ['x', 'y', 'z'], ['z', 't', 'z']],
[['k', 't', 'x'], ['z', 't', 'z'], ['x', 'y', 'z']]]
您的代码可能如下所示:
my_list = [['x', 'y', 'z'], ['z', 't', 'z'], ['k', 't', 'x']]
print('My list: {}'.format(my_list))
all_permutations = [list(p) for p in itertools.permutations(my_list)]
for p in all_permutations:
print('Possible permutation: {}'.format(p))
my_list = [['x', 'y', 'z'], ['z', 't', 'z'], ['k', 't', 'x']]
print('My list: {}'.format(my_list))
all_permutations = [list(p) for p in itertools.permutations(my_list)]
for p in all_permutations:
print('Possible permutation: {}'.format(p))