【发布时间】:2015-05-14 03:42:30
【问题描述】:
我知道如何使用这些字符串将字符串列表拆分为嵌套列表,但我不确定现在如何将这些字符串拆分为多个字符串。
例如:
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']]
有什么办法可以解决这个问题吗?
【问题讨论】:
-
旁注:
content = f.readlines()会更简单、更高效。不过,还有一个更简单、更有效的解决方案(例如,请参阅我的答案)。
标签: python string list nested-lists string-split