I am writing a code in Python 2.7 in which I have defined a list of strings. I then want to search this list\'s elements for a set of letters. These letters must be in rando
for item in wordlist:
for character in letters:
if character in item:
print item
break
Here goes your solution:
for item in wordlist:
b = False
for c in letters:
b = b | (item.find(c) != -1)
if b:
print item
Using sets and the in syntax to check.
wordlist = ['mississippi','miss','lake','que']
letters = set('aqk')
for word in wordlist:
if word in letters:
print word
[word for word in wordlist if any(letter in word for letter in 'aqk')]
It would be easy using set():
wordlist = ['mississippi','miss','lake','que']
letters = set('aqk')
for word in wordlist:
if letters & set(word):
print word
Output:
lake
que
Note: The &
operator does an intersection between the two sets.