我有一个列表:
a_list = ["apple", "pear", "grape", "soil"]
我想要元素和无序对:
["apple", "apple pear", "apple grape", "apple soil", "pear", "pear grape", "pear soil", "grape", "grape soil", "soil"]
答案 0 :(得分:1)
这是使用itertools.combinations
的选项:
from itertools import combinations
a_list = ["apple", "pear", "grape", "soil"]
res = a_list + list(" ".join(comb) for comb in combinations(a_list, 2))
# ['apple', 'pear', 'grape', 'soil', 'apple pear', 'apple grape', 'apple soil',
# 'pear grape', 'pear soil', 'grape soil']