简体   繁体   中英

Replacing Vowels and Consonants Using List Comprehension Python

I'm trying to replace all of the letters in each element of a list of strings into either "C" for consonant or "V" for vowel. For example, if I were to have ["the", "cat"], I would want it to return ["CCV", "CVC"].

Is there a way for me to do this using list comprehension? If so, how?

This will do:

new_list = [''.join(('V' if char in {'a', 'e', 'i', 'o', 'u'} else 'C') for char in string) for string in previous_list]

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