繁体   English   中英

获取列表的所有可能组合并将其保存在内存中

[英]Get all possible combinations of lists and save it in memory

我需要帮助解决以下问题:

我有一个列表,我想获得该列表的所有可能组合(但不要混淆子列表项的顺序,也不要将一个子列表的项与另一个混合)。 所以这是我到目前为止的代码:

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']]
print("my list is", my_list)
for x in itertools.permutations(my_list):
    y.append(list(x))
    print(y)
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 combinations of my_list")
else:
     print("Oops! Try again!")

你得到:

Yeah! new_list is one of the combinations of my_list

如果您不使用循环,而是使用其他答案中所示的列表理解,您的代码甚至可以更加 Pythonic。

您可以替换:

for x in itertools.permutations(my_list):
    y.append(list(x))
    print(y)

和:

y = [list(x) for x in 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(itertools.permutations(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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM