![](/img/trans.png)
[英]How to remove special character in a list of string and split it into separate elements
[英]In a list, how to separate each string (with mixed special characters) into a single character?
假设我想将以下列表拆分为单个字符。
mylist = [('dog', 'camel'), ('horse'), ('List_of_people_saved_by_Oskar'), 'mouse_bear', 'lion tiger rabbit', 'ant']
到目前为止,这是我尝试过的:
L1 = [animal for word in mylist for animal in word.split('_')]
print(L1)
输出应如下所示:
`['dog', 'camel', 'horse', 'List', 'of', 'people', 'saved', 'by', 'Oskar', 'mouse', 'bear', 'lion', 'tiger' 'rabbit', 'ant']`
但我收到一个错误:
AttributeError: 'tuple' object has no attribute 'split'
您可以使用re.findall(r'[^_ ]+', word)
代替下划线或空格分隔的单词。 还添加另一个理解层以平整可能的字符串元组:
import re
L1 = [animal for item in mylist for word in (item if isinstance(item, (tuple, list)) else (item,)) for animal in re.findall(r'[^_ ]+', word)]
L1
将变为:
['dog', 'camel', 'horse', 'List', 'of', 'people', 'saved', 'by', 'Oskar', 'mouse', 'bear', 'lion', 'tiger', 'rabbit', 'ant']
您只是混淆了去哪里。
[animal.split('_') for word in mylist for animal in word]
还有一个额外的问题是("horse")
不是元组。 ("horse",)
是。 因此, ("horse")
在括号中仅是"horse"
, for animal in word
将枚举"horse"
的各个字母,而不是退还一只"horse"
动物。
如果希望除_
以外的其他字符,可以使用re.split
和一个字符类:
import re
[re.split(r'[_ ]', animal) for word in mylist for animal in word]
如果您实际上打算使非成对的动物不成为元组,那么您将必须专门处理以下情况:
[re.split(r'[_ ]', animal)
for word in mylist
for animal in (word if isinstance(word, tuple) else (word,))]
好吧,这是一个更具可读性的代码,因为我实在不喜欢拥有内联代码的想法,无论它有多高效或更快。 同样,这可能会使您更容易理解,并且不需要导入任何库。
码:
mylist = [('dog', 'camel'), ('horse'), ('List_of_people_saved_by_Oskar'), 'mouse_bear', 'lion tiger rabbit', 'ant']
new_list = []
for items in mylist:
if type(items) == tuple:
for animals in items:
new_list.append(animals)
elif '_' in items:
new_animal = items.split('_')
for animals in new_animal:
new_list.append(animals)
elif ',' in items:
new_animal = items.split(',')
for animals in new_animal:
new_list.append(animals)
elif ' ' in items:
new_animal = items.split(' ')
for animals in new_animal:
new_list.append(animals)
else:
new_list.append(items)
print(new_list)
输出:
['dog', 'camel', 'horse', 'List', 'of', 'people', 'saved', 'by', 'Oskar', 'mouse', 'bear', 'lion', 'tiger', 'rabbit', 'ant']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.