Erna Piantari
Erna Piantari

Reputation: 627

How to Compare Frozenset with list of tuple in Python

I have a frozenset A and list B:

>>> a=frozenset(['A','B'])
>>> b=[('A','B'),('C',)]
>>> a in b
False #  my output expectation is True

>>> a=frozenset(['A','B'])
>>> b=[('A',),('B',)]
>>> a in b
False # as my output expectation 

I want to compare and indicate that value of frozenset a in b is True. What sholud I do?

Upvotes: 1

Views: 406

Answers (1)

Łukasz Rogalski
Łukasz Rogalski

Reputation: 23223

Since sets are unordered, you need to have a way to ensure correct ordering. In your case this'll work:

tuple(sorted(frozenset(['A','B']))) in [('A','B'),('C')]

Although you may want to create custom key function for more complex cases.

Upvotes: 1

Related Questions