简体   繁体   中英

How do I find words from a list with specific letters?

first list:

word_list = ['Jack','Caroline','Jane']

second list:

letter_list = ['a','c']

I want to save all the words that contain the letters 'a' AND 'c' in a third list.

The third list should look like this:

third_list = ['Jack','Caroline']

You could try this:

word_list = ['Jack', 'Caroline', 'Jane']
letter_list = ['a', 'c']

third_list = [word for word in word_list if all(letter in word.lower() for letter in letter_list)]
print(third_list)
# ['Jack', 'Caroline']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM