【发布时间】:2019-03-20 16:43:07
【问题描述】:
假设,我想将以下列表拆分为单个字符。
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'
【问题讨论】:
-
什么算作特殊字符?
-
对不起,我没有找到你
-
除了空格(
' ')和下划线('_')还有什么? -
只有
Space和Underscore存在。除此之外,该列表可能还有成对的字符串,例如('dog', 'camel')。 -
您希望代码特定于给定的输入或任何它的方式?另外,输入的内容可以与您已经提供的不同吗?
标签: python string python-3.x list nltk