0

Check if something from a list appears twice in a string

I have a code that checks if something from a list is in string: if any(x in string for x in list) But how do I check if it appears twice or more than twice

27th May 2020, 3:28 PM
Kirill
Kirill - avatar
8 Réponses
+ 6
any(string.count(x)>=2 for x in lst) Will be true if at least 1 item in lst appears twice or more in string.
27th May 2020, 3:52 PM
Louis
Louis - avatar
+ 5
Kirill Vidov Something like this: s = 'Sololearn' l = ['a', 'o', 'l', 'r', 'n'] print([x for x in l if s.count(x) >= 2]) # will only print 'o' and 'l'
27th May 2020, 5:02 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
Thank you everyone for the help
27th May 2020, 6:11 PM
Kirill
Kirill - avatar
+ 1
Kirill Vidov Show ur attempt
27th May 2020, 3:30 PM
Abhay
Abhay - avatar
+ 1
Kuba Siekierzyński can you please help if you can?
27th May 2020, 3:47 PM
Kirill
Kirill - avatar
+ 1
Exactly, I would use count() to determine if there is more than one occurence of an element in a collection.
27th May 2020, 4:06 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
Abhay if any(x in string for x in list) >= 2, but that doesn't work
27th May 2020, 3:32 PM
Kirill
Kirill - avatar