Search code examples
pythonlistcomparison

Compare words ignoring certain characters - Python


I've seen a couple of questions similar to this but none in python. Basically, I want to check if certain words are in a list. Though the words I want to compare might have a ',' which I want to ignore. I have tried this, though it does not ignore the ','.

x = ['hello','there,','person']
y = ['there','person']
similar = [words for words in x if words in y ]
print(similar)

Output

['person']

But I want

['there','person']

Does anyone know the simplest way of implementing this?


Solution

  • Check out this code use any function along with map to map containing conditon.

    x = ['hello','there,','person']
    y = ['there','person'] # or take this for more intuation ['there','person','bro']
    similar = [words for words in y if any(map(lambda i: i.count(words), x))]
    print(similar)
    

    OUTPUT:

    ['there', 'person']