Vincent Vincent - 9 months ago 48
Python Question

Python : if any(x in y), case and accent insensitive?

I have the following list of strings :

exclude = ['eee', 'iii']

I have a word to be tested :

word = 'Iîïe'

I want the following test to be true :

if any(x in word for x in exclude):
#I want to be here !

In order to be true, my condition needs to be case-insensitive and accent-insensitive... How?


You can use a third party package called unidecode:

What Unidecode provides is a middle road: function unidecode() takes Unicode data and tries to represent it in ASCII characters (i.e., the universally displayable characters between 0x00 and 0x7F), where the compromises taken when mapping between two character sets are chosen to be near what a human with a US keyboard would choose.


from unidecode import unidecode
if any(x in unidecode(word).lower() for x in exclude):