python - 列表中 Python 中的无序对(对集)

标签 python python-itertools

<分区>

我有一个项目列表

 alist = ['dog', 'cat', 'fish']

我想返回所有唯一的无序对,所以在这种情况下:

 (dog,cat)(dog,fish)(fish,cat)

itertools.combinations 没有考虑无序条件,所以它不是我需要的。

最佳答案

itertools 的问题出在哪里?

import itertools

alist = ['dog', 'cat', 'fish']
for result in itertools.combinations(alist, 2):
    print result

输出:

('dog', 'cat')
('dog', 'fish')
('cat', 'fish')

关于python - 列表中 Python 中的无序对(对集),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35047737/

相关文章:

python - 如何将Python版本降级到Python 2.7.13(默认,2017年11月7日,00 :42:48)?

python - 具有多个列表和条件的排列

python - 如何确定用户输入是奇数还是偶数?

python - 我可以删除 django 中 url 中的前导零吗?

python - 检查二维列表的索引并根据二维列表打印索引

python - 是否可以在 python 中 pickle itertools.product?

python - 多参数Python映射

python - 在 Python 中使用计数器重复创建文件

python - 在python中迭代两个不同大小的列表

python - 使用函数从指定索引开始对 Python 中不同长度的列表求和