Unordered Pairs (pair sets) in Python from a list [closed]

邮差的信 提交于 2021-02-04 18:30:06

问题


I have a list of items

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

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

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

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


回答1:


Where is your problem with itertools?

import itertools

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

output:

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


来源:https://stackoverflow.com/questions/35047737/unordered-pairs-pair-sets-in-python-from-a-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!