如何在Python中的嵌套列表中拆分字符串?

时间:2015-03-12 04:48:48

标签: python string list nested-lists string-split

我知道如何使用这些字符串将字符串列表拆分为嵌套列表,但我不确定如何将这些字符串拆分为多个字符串。

例如:

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']]

我有办法解决这个问题吗?

3 个答案:

答案 0 :(得分:2)

如果,比方说,

x = [['these are some words'], ['these are some more words'], ['these are even more words'], ['these are the last words']]

然后

 y = [sublist[0].split() for sublist in x]

会给你

[['these', 'are', 'some', 'words'], ['these', 'are', 'some', 'more', 'words'], ['these', 'are', 'even', 'more', 'words'], ['these', 'are', 'the', 'last', 'words']]

根据需要。

但是,如果你的原始表达

contentLists = [content[i:i+1] for i in range(0, len(content), 1)]

生成我在这里称为x的列表,这是毫无意义的 - 为什么要建立一个长度为1的子列表列表?!

直接看起来像你想要的那样:

y = [item.split() for item in content]

而不是从contentLists生成x,然后生成y,不是吗?

答案 1 :(得分:1)

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可以为您完成。

答案 2 :(得分:0)

您可以通过以下有效方式实现您想要的目标:

with open(file_path) as input_file:
    content_lists = [line.split() for line in input_file]

实际上,f.read()首先将整个文件加载到内存中,然后.splitlines()创建一个分割成行的副本:不需要这两个数据结构,因为您只需读取文件行按行和依次拆分每条线,如上所述。这更有效,更简单。