python - 如何在不使用 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 的情况下获得相同的输出?
解决方案
也许试试这个
l = [1, 2, 3]
for i in l:
for j in l:
if(j <= i):
continue
else:
print(i, j)
推荐阅读
- laravel - 非法指令:4 执行 php artisan migrate 时
- javascript - 有没有办法在大 html 字符串中获取元素的确切 indexOf
- intellij-idea - 有没有办法在调试/不调试时反映 java 项目(在 Intellij 中)中所做的更改而不必重建项目
- node.js - 如何防止 JWT 令牌过载
- node.js - 无法从服务器 api 或客户端代码中检测到超时错误?使用 nodejs 与 postman 进行测试
- c# - 如何在 C# 中使用 OpenSSL
- vue.js - “TypeError:filesys.existsSync 不是函数”
- reporting-services - 将 SSRS Tablix 过滤成 2 列,为菜单页面链接动态更新
- java - 如何在相对布局或任何布局中动态添加视图
- cmake - 如何在构建文件夹之外运行 CTest?