牧羊人nacy
看看迭代工具中的组合。>>> l = [(1,2), (1,3), (1,4), (2,3), (2, 4), (3,4)]>>> import itertools>>> combinations = itertools.combinations(l, 2)>>> for combination in combinations:... print(combination)...((1, 2), (1, 3))((1, 2), (1, 4))((1, 2), (2, 3))((1, 2), (2, 4))((1, 2), (3, 4))((1, 3), (1, 4))((1, 3), (2, 3))((1, 3), (2, 4))((1, 3), (3, 4))((1, 4), (2, 3))((1, 4), (2, 4))((1, 4), (3, 4))((2, 3), (2, 4))((2, 3), (3, 4))((2, 4), (3, 4))
红颜莎娜
试试这个代码:from itertools import combinations # Get all combinations and length 2 comb = combinations([(1,2), (1,3), (1,4), (2,3), (2, 4), (3,4)], 2) # Print the obtained combinations for i in list(comb): print(i[0],",", i[1])输出:(1, 2) , (1, 3)(1, 2) , (1, 4)(1, 2) , (2, 3)(1, 2) , (2, 4)(1, 2) , (3, 4)(1, 3) , (1, 4)(1, 3) , (2, 3)(1, 3) , (2, 4)(1, 3) , (3, 4)(1, 4) , (2, 3)(1, 4) , (2, 4)(1, 4) , (3, 4)(2, 3) , (2, 4)(2, 3) , (3, 4)(2, 4) , (3, 4)