python - Membership testing on set of pairs, how do i make the order of element in pairs irrelevant? -


here want do:

m1 = (a,b) m2 = (c,d) bad_combos = set() bad_combos.add((m1,m2)) #((a,b),(c,d)) ... #adding many other elements in set  #when this: if (m2,m1) in bad_combos:     print("found") else:     print("not found")  #the result "not found" 

is there way can make order of elements in pair irrelevant when o membership testing:

bad_combos.add((m3,m4))  if (m4,m3) in bad_combos:     #this return true? 

any idea appreciated! in advance!

one option have (if bad_combos must stay set) adding frozensets set , checking if frozenset of pairs exists:

m1 = ('a','b') m2 = ('c','d') bad_combos = set() bad_combos.add(frozenset([m1,m2]))   (m2, m1) in bad_combos  # false  frozenset([m2, m1]) in bad_combos  # true 

this, of course, retains o(1) complexity membership testing.

another option (if sets aren't mandatory) involves switching list storing data structure , adding set pairs it:

m1 = ('a','b') m2 = ('c','d') bad_combos = [] bad_combos.append({m1,m2}) #((a,b),(c,d))  if {m2,m1} in bad_combos:     print("found") else:     print("not found") 

this, of course, results in o(n) membership testing.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -