Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case-insensitive set intersection

Tags:

python

What would be the best way to do the following case-insensitive intersection:

a1 = ['Disney', 'Fox']
a2 = ['paramount', 'fox']
a1.intersection(a2)
> ['fox']

Normally I'd do a list comprehension to convert both to all lowercased:

>>> set([_.lower() for _ in a1]).intersection(set([_.lower() for _ in a2]))
set(['fox'])

but it's a bit ugly. Is there a better way to do this?

like image 652
David542 Avatar asked Jun 18 '26 16:06

David542


1 Answers

Using the set comprehension syntax is slightly less ugly:

>>> {str.casefold(x) for x in a1} & {str.casefold(x) for x in a2}
{'fox'}

The algorithm is the same, and there is not any more efficient way available because the hash values of strings are case sensitive.

Using str.casefold instead of str.lower will behave more correctly for international data, and is available since Python 3.3+.

like image 75
wim Avatar answered Jun 20 '26 06:06

wim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!