【问题标题】:Remove all elements from a list called friends, whose names do not end in a vowel [duplicate]从名为friends的列表中删除所有元素,其名称不以元音结尾[重复]
【发布时间】:2021-07-25 08:18:16
【问题描述】:

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

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']

【问题讨论】:

标签: python list-comprehension


【解决方案1】:

这应该可行->

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']

【讨论】:

  • @Tomerikoo 是的,它正在删除那些名称不以元音结尾的元素。
猜你喜欢
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 2015-03-02
相关资源
最近更新 更多