我知道如何使用这些字符串将字符串列表拆分为嵌套列表,但是我不确定如何将这些字符串拆分为多个字符串。
例如:
def inputSplit(file_name):
with open(file_name) as f:
content = f.read().splitlines()
i = 0
contentLists = [content[i:i+1] for i in range(0, len(content), 1)]
会给我类似的东西:
[['these are some words'], ['these are some more words'], ['these are even more words'], ['these are the last words']]
我不确定如何使用字符串拆分使输出看起来像这样:
[['these', 'are', 'some', 'words'], ['these', 'are', 'some', 'more', 'words'], ['these', 'are', 'even', 'more', 'words'], ['these', 'are', 'the', 'last', 'words']]
有办法解决这个问题吗?
x=[['these are some words'], ['these are some more words'], ['these are even more words'], ['these are the last words']]
print [i[0].split() for i in x]
输出:[['these', 'are', 'some', 'words'], ['these', 'are', 'some', 'more', 'words'], ['these', 'are', 'even', 'more', 'words'], ['these', 'are', 'the', 'last', 'words']]
简单list comprehension
可以为您做到。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句