【发布时间】:2020-01-15 04:55:08
【问题描述】:
我有一个包含 5 个元素的列表(我们称之为“列表”):
['sympathize.', 'sufficient.', 'delightful.', 'contrasted.', 'unsatiable']
我想从列表的每个项目中删除 元音 (vowels = ('a', 'e', 'i', 'o', 'u')),最终结果应该是这样
没有元音的列表:
['sympthz.', 'sffcnt.', 'dlghtfl.', 'cntrstd.', 'nstbl']
有什么想法吗?提前致谢。
我的代码:
list = ['sympathize.', 'sufficient.', 'delightful.', 'contrasted.','unsatiable']
vowels = ('a', 'e', 'i', 'o', 'u')
for x in list.lower():
if x in vowels:
words = list.replace(x,"")
输出:
AttributeError: 'list' object has no attribute 'lower'
【问题讨论】:
-
嗯,list没有lower属性,可以调用x.lower()
-
不要使用名称
list,因为它是 Python 中的内置类型。 -
words = ['sympathize.', 'sufficient.', 'delightful.', 'contrasted.','unsatiable'] vowels = ('a', 'e', 'i', 'o', 'u') for x in words.lower(): if x in vowels: words = words.replace(x,"")
-
我得到了同样的错误(for x in words.lower(): AttributeError: 'list' object has no attribute 'lower')