问题
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