你这个是“组合”问题,使用Python标准库itertools可以轻松解决:
import itertools def combination(items, num): return list(itertools.combinations(items, num))
>>> from itertools import combinations >>> test = combinations([1,2,3,4], 2) >>> for el in test: print el (1, 2) (1, 3) (1, 4) (2, 3) (2, 4) (3, 4)