繁体   English   中英

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

[英]How do I split strings within nested lists in Python?

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

例如:

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

然后

 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 ,不是吗?

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可以帮助您。

您可以像这样高效地实现所需的目标:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM