python - 如何仅过滤排列中的唯一对,python?
问题描述
from itertools import permutations
list0 = [1, 2, 3]
for el1, el2 in permutations(list0, r=2):
print(el1, el2)
输出:
1 2
1 3
2 1
2 3
3 1
3 2
此输出包含所有可能的排列,我需要唯一的数字组合,我应该如何过滤它们,也许我应该使用另一个代码。我需要
1 2
1 3
2 3
解决方案
您可以使用combinations
而不是permutations
来实现
from itertools import combinations
list0 = [1, 2, 3]
for combination in combinations(list0, 2):
print(combination)
推荐阅读
- java - Java中的Web抓取
- assembly - 尝试在 Assembly 和 C 中关闭时机器崩溃
- python - 通过另一组保持单词顺序将单词列表列入白名单的最快方法
- c# - 程序集集合未在 AssemblyLoadContext 中显示共享程序集
- flutter - 如何使 adobe XD 生成的代码到颤振插件,根据屏幕尺寸实际可用和响应?
- spring - 在 Spring AOP 中获取请求/响应正文和标头
- node.js - PATH变量在电子中为空?
- python - 如何使用熊猫从数据框中删除多列(使用列名)?
- python - 过滤数据框,根据某些列的值显示行
- r - grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, 中的警告:字符 0x20 的字体宽度未知