Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 511
Chris J. Vargo Avatar asked Dec 24 '22 09:12

Chris J. Vargo


1 Answers

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')
like image 77
Prune Avatar answered Jan 14 '23 00:01

Prune