从名为friends的列表中删除所有元素,这些元素的名称不以元音结尾

2024-05-20 10:25:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用列表理解。已尝试多次迭代,仅显示当前一次

friends = ['Alice', 'Bob', 'Charlie', 'Derek']

cons_ends = [i for i in friends if i[-1] != ("a", "e", "i", "o", "u")]

print(cons_ends)

['Alice', 'Bob', 'Charlie', 'Derek']

Tags: in列表forifderekbobprintalice
1条回答
网友
1楼 · 发布于 2024-05-20 10:25:47

这应该有效->

friends = ['Alice', 'Bob', 'Charlie', 'Derek']
friends = [x for x in friends if x[-1].lower() in ['a','e','i','o','u']]
print(friends)

产出->

['Alice', 'Charlie']

相关问题 更多 >