从字符串列表中删除元音
Removing vowels from a list of strings
我有一个包含 5 个元素的列表(我们称之为 "list"):
['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'
试试这个:
mylist = ['sympathize.', 'sufficient.', 'delightful.', 'contrasted.','unsatiable']
vowels = ['a', 'e', 'i', 'o', 'u']
for i in range(len(mylist)):
for v in vowels:
mylist[i] = mylist[i].replace(v,"")
print(mylist)
这是使用 str.maketrans
的一种方法:
l = ['sympathize.', 'sufficient.', 'delightful.', 'contrasted.', 'unsatiable']
table = str.maketrans('', '', 'aeiou')
[s.translate(table) for s in l]
# ['sympthz.', 'sffcnt.', 'dlghtfl.', 'cntrstd.', 'nstbl']
我有一个包含 5 个元素的列表(我们称之为 "list"):
['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'
试试这个:
mylist = ['sympathize.', 'sufficient.', 'delightful.', 'contrasted.','unsatiable']
vowels = ['a', 'e', 'i', 'o', 'u']
for i in range(len(mylist)):
for v in vowels:
mylist[i] = mylist[i].replace(v,"")
print(mylist)
这是使用 str.maketrans
的一种方法:
l = ['sympathize.', 'sufficient.', 'delightful.', 'contrasted.', 'unsatiable']
table = str.maketrans('', '', 'aeiou')
[s.translate(table) for s in l]
# ['sympthz.', 'sffcnt.', 'dlghtfl.', 'cntrstd.', 'nstbl']