Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取列表中所有可能的值组合--Python_Python_Python 3.x_Combinations_Itertools - Fatal编程技术网

获取列表中所有可能的值组合--Python

获取列表中所有可能的值组合--Python,python,python-3.x,combinations,itertools,Python,Python 3.x,Combinations,Itertools,我有一个包含['a'、'bill'、'smith']的列表,我想编写一个python代码,以获得应用特定标准的所有可能组合。更准确地说,我希望获得列表中这三个元素的组合,以及每个元素的第一个字母(如果该元素尚未出现在输出列表中)。例如,给定列表['a',bill',smith'],预期输出的一部分将是:['a',bill',smith'],['bill',smith'],['a',smith'],['a',smith']但是,['a',b',smith'],['bill',s'],['a',s'

我有一个包含
['a'、'bill'、'smith']
的列表,我想编写一个python代码,以获得应用特定标准的所有可能组合。更准确地说,我希望获得列表中这三个元素的组合,以及每个元素的第一个字母(如果该元素尚未出现在输出列表中)。例如,给定列表
['a',bill',smith']
,预期输出的一部分将是:
['a',bill',smith'],['bill',smith'],['a',smith'],['a',smith']
但是,
['a',b',smith'],['bill',s'],['a',s']
。我不希望得到这样的输出
['s',bill',smith']
,因为第一个元素已经被第三个元素('smith')考虑进去了。有人能帮我吗

这就是我到目前为止所做的:

mapping = dict(enumerate(['a', 'bill', 'smith']))
for i in mapping.items():
if len(i[1])>1:
    mapping[i[0]] = [i[1], i[1][0]]
else:
    mapping[i[0]] = [i[1]]

print(mapping)
{0: ['a'], 1: ['bill', 'b'], 2: ['william', 'w'], 3: ['stein', 's']}
我现在累坏了。我想使用itertools库对dict值进行迭代,以创建所有可能的组合


提前感谢:)

您可以使用一些
itertools

from itertools import product, permutations

lst = [list({s, s[:1]}) for s in ['a', 'bill', 'smith']]
# [['a'], ['bill', 'b'], ['s', 'smith']]

for perms in map(permutations, product(*lst)):
    for p in perms:
        print(p)

('a', 'bill', 's')
('a', 's', 'bill')
('bill', 'a', 's')
('bill', 's', 'a')
('s', 'a', 'bill')
('s', 'bill', 'a')
('a', 'bill', 'smith')
('a', 'smith', 'bill')
('bill', 'a', 'smith')
('bill', 'smith', 'a')
('smith', 'a', 'bill')
('smith', 'bill', 'a')
('a', 'b', 's')
('a', 's', 'b')
('b', 'a', 's')
('b', 's', 'a')
('s', 'a', 'b')
('s', 'b', 'a')
('a', 'b', 'smith')
('a', 'smith', 'b')
('b', 'a', 'smith')
('b', 'smith', 'a')
('smith', 'a', 'b')
('smith', 'b', 'a')
第一步创建等效列表的列表:

[['a'], ['bill', 'b'], ['s', 'smith']]
然后,
product
生成所述列表中的列表的笛卡尔乘积:

('a', 'bill', 's')
('a', 'bill', 'smith')
('a', 'b', 's')
...
对于其中的每一种,
排列
为您提供了所有排列:

('a', 'bill', 's')
('a', 's', 'bill')
('bill', 'a', 's')
...

您可以通过
itertools
中的
组合执行类似操作:

from itertools import product, permutations

lst = [list({s, s[:1]}) for s in ['a', 'bill', 'smith']]
# [['a'], ['bill', 'b'], ['s', 'smith']]

for perms in map(permutations, product(*lst)):
    for p in perms:
        print(p)

('a', 'bill', 's')
('a', 's', 'bill')
('bill', 'a', 's')
('bill', 's', 'a')
('s', 'a', 'bill')
('s', 'bill', 'a')
('a', 'bill', 'smith')
('a', 'smith', 'bill')
('bill', 'a', 'smith')
('bill', 'smith', 'a')
('smith', 'a', 'bill')
('smith', 'bill', 'a')
('a', 'b', 's')
('a', 's', 'b')
('b', 'a', 's')
('b', 's', 'a')
('s', 'a', 'b')
('s', 'b', 'a')
('a', 'b', 'smith')
('a', 'smith', 'b')
('b', 'a', 'smith')
('b', 'smith', 'a')
('smith', 'a', 'b')
('smith', 'b', 'a')
这里,我假设你想要列表中每个单词的第一个字母,只要它的长度大于1。如果不是,您可以更改If条件

from itertools import combinations

lst = ['a', 'bill', 'smith']
lst_n=[]
for words in lst:
    lst_n.append(words)
    if len(words)>1:
        lst_n.append(words[0])

for t in range(1,len(lst_n)+1):
    for comb in combinations(lst_n,r=t):
        print(list(comb))
输出:

['a']
['bill']
['b']
['smith']
['s']
['a', 'bill']
['a', 'b']
['a', 'smith']
['a', 's']
['bill', 'b']
['bill', 'smith']
['bill', 's']
['b', 'smith']
['b', 's']
['smith', 's']
['a', 'bill', 'b']
['a', 'bill', 'smith']
['a', 'bill', 's']
['a', 'b', 'smith']
['a', 'b', 's']
['a', 'smith', 's']
['bill', 'b', 'smith']
['bill', 'b', 's']
['bill', 'smith', 's']
['b', 'smith', 's']
['a', 'bill', 'b', 'smith']
['a', 'bill', 'b', 's']
['a', 'bill', 'smith', 's']
['a', 'b', 'smith', 's']
['bill', 'b', 'smith', 's']
['a', 'bill', 'b', 'smith', 's']

在这里,如果您希望组合的长度为
3
仅删除
for loop
range
并设置
r=3

使用标准
itertools
工具生成所有组合,然后过滤掉您的标准认为冗余的元素是否不可行?然后。。。根据python,如何说标准是多余的?无论如何,谢谢:)但是@schwobaseggl解决了很多问题!你救了我,谢谢你花时间帮助我,但我发现其他答案更有用:)