简体   繁体   中英

how to consolidate an if-statement with multiple conditions in python 3.8?

I'm trying to take a list of words and print out another list of every word that has all 5 written vowels 'aeiou'.

print([word for word in word_list if 'a' in word and 'e' in word and 'i' in word and 'o' in word and 'u' in 
word])

For example, 'equation', 'consequential', and 'authorize' would be in the output list.

How can I consolidate all the conditions in the if-statement?

This works:

print([word for word in word_list if all(letter in word for letter in "aeiou")])

Test live example on onlinegdb .

Basically, the conditions follow a pattern so you can factorize them in a test "for each character in the string 'aeiou', test if character is in main string".

all(conditions) is to be used since you want all of them to be true at the same time, because of and (for or you could use any instead of all )

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