vowel = ['a','e','i','o','u']
my_list = ['hbyl','hawk','ibzj','hcxk','gawk']
my_list中的单词。如果单词没有一个元音。从列表中删除单词
for word in my_list:
if word does not include at least one vowel:
my_list.remove(word)
print (my_list)
['hawk','ibzj','gawk']
答案 0 :(得分:3)
这应该有效! :)
vowel = ['a','e','i','o','u']
my_list = ['hbyl','hawk','ibzj','hcxk','gawk']
vowset = set(vowel)
[word for word in my_list if vowset.intersection(word)]
答案 1 :(得分:1)
您不应在迭代时删除元素。相反,构造一个新列表,最好使用列表理解:
vowel = ['a','e','i','o','u']
my_list = ['hbyl','hawk','ibzj','hcxk','gawk']
new_my_list = [word for word in my_list if any(v in word for v in vowel)]
本质上,列表理解所说的是“仅当该单词中有任何元音时,才包含任何给定的单词。”
答案 2 :(得分:0)
您可以将函数filterfalse()
与设置的方法isdisjoint()
一起使用:
from itertools import filterfalse
vowel = ['a','e','i','o','u']
my_list = ['hbyl','hawk','ibzj','hcxk','gawk']
vowel= set(vowel)
list(filterfalse(lambda x: vowel.isdisjoint(x), my_list))
# ['hawk', 'ibzj', 'gawk']