列表中Python中的无序对(对集) [英] Unordered Pairs (pair sets) in Python from a list

查看:67
本文介绍了列表中Python中的无序对(对集)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个物品清单

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

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

I want to return all unique unordered pairs, so in this case:

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

itertools.combinations没有考虑到无序状态,所以这不是我所需要的.

itertools.combinations does not take into account the unordered condition, so it is not quite what I need.

推荐答案

itertools 哪里出问题?

import itertools

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

输出:

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

这篇关于列表中Python中的无序对(对集)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆