I have a list as below
[('generators', 'generator'), ('game', 'games'), ('generator', 'generators'), ('games', 'game'), ('challenge', 'challenges'), ('challenges', 'challenge')]
Pairs ('game', 'games') and ('games', 'game') are kind of same but they are in different order.
The output I am trying to achieve
[('generators', 'generator'), ('games', 'game'), ('challenge', 'challenges')]
How can I remove pairs as such from above list ?
Any suggestions, greatly appreciated.
解决方案
You can use an unordered hashable data structure within a set. frozenset() is your friend here:
In [7]: {frozenset(i) for i in your_list}
Out[7]:
{frozenset({'generator', 'generators'}),
frozenset({'game', 'games'}),
frozenset({'challenge', 'challenges'})}
Note that in order to avoid looping over your list it's better to do this at the first place while creating your list.