首页 > 解决方案 > 如何在不使用 itertools 导入组合的情况下从列表中获取所有项目对

问题描述

我有一个列表,我想要列表中所有可能的项目组合。

from itertools import combinations
l = [1,2,3]
for i in combinations(l,2):
    print(i) // (1, 2) (1,3) (2,3)

我想要相同的输出,但不使用 itertools。我试过:

l = [1,2,3]
for i in l:
    for j in l:
        print((i,j)) // (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)

如何在不使用 itertools 的情况下获得相同的输出?

标签: pythonpython-3.x

解决方案


也许试试这个

l = [1, 2, 3]
for i in l:
    for j in l:
        if(j <= i):
            continue
        else:
            print(i, j)

推荐阅读