说我有类似的东西:
array = ['apple', 'banana', 'carrot', 'dragonfruit', 'eggplant']
现在,我想在此数组中打印值,该值不是以元音开头的。
IE
banana
carrot
dragonfruit
我能行:
for str in array:
if str[0].lower() not in ('a','e','i','o','u'):
print str
我想知道的是,是否有一种像这样的Python语言:
for str in array where str[0].lower() not in ('a','e','i','o','u'):
您知道str.startswith接受一个前缀元组吗?
执行
for fruit in (elem for elem in array
if not elem.startswith(('a','e','i','o','u'))):
print fruit
输出
banana
carrot
dragonfruit
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句