Python 3.x 如何在python中从大小为n的子列表中查找组合

Python 3.x 如何在python中从大小为n的子列表中查找组合,python-3.x,Python 3.x,我有一张单子 三角形=['0,3,2','0,3,1','0,2,3','1,3,0','1,3,5'] 我想得到一个 0,2,3 #since 0,3,2 would be the same combination 0,3,1 1,3,5 我使用下面的代码 from itertools import combinations for comb in combinations(triangle, 3): print comb 然而,我的结果是3对,即 '0, 3, 2', '0

我有一张单子

  • 三角形=['0,3,2','0,3,1','0,2,3','1,3,0','1,3,5']
我想得到一个

0,2,3  #since 0,3,2 would be the same combination
0,3,1
1,3,5
我使用下面的代码

from itertools import combinations
for comb in combinations(triangle, 3):
    print comb
然而,我的结果是3对,即

'0, 3, 2', '0, 3, 1', '0, 2, 3'
etc.
我也尝试过改变这种情况

for comb in combinations(triangle, 3):
              to
for comb in combinations(triangle, 1):

但是,组合不是唯一的

您有一个字符串列表。你可以这样做:

triangle = ['0, 3, 2', '0, 3, 1', '0, 2, 3','1, 3, 0', '1, 3, 5']
list(map(', '.join, set(tuple(sorted(s.split(', '))) for s in triangle)))
# ['1, 3, 5', '1, 3, 0', '2, 3, 0']

您可以使用带有冻结集的字典作为键来有效地消除重复数据,然后获取其值列表,例如:

triangle = ['0, 3, 2', '0, 3, 1', '0, 2, 3','1, 3, 0', '1, 3, 5']
output = list({frozenset(el.split(', ')): el for el in triangle}.values())
给你:

['0, 2, 3', '1, 3, 0', '1, 3, 5']

确保:你的
三角形
应该是一个由3个数字组成的逗号分隔字符串列表?你能更好地解释这个问题吗?因此,您想查找“0,3,2”,因为列表中也存在它的组合?如果有重复,您介意我选择哪个组合吗?(为什么
'0,2,3'
而不是
'0,3,2'
?)@Adam.Er8我的意思是包括我将列表从最小到最大排序的部分