How do I split strings within nested lists in Python?

岁酱吖の 提交于 2021-02-04 06:21:27

问题


I know how to split a list of strings into a nested list using those strings, but I'm not specifically sure how I would go about splitting those strings now into multiple strings.

For example:

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

Would give me something like:

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

I'm not sure how to use the string split to make my output look like this:

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

Is there a way I can go about this?


回答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]

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

Simple list comprehension can do it for you.




回答2:


If, say,

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

then

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

will give you

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

as desired.

However, if your original expression

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

produces the list I've called x here, it's pretty pointless -- why build a list of sub-lists each of length 1 in the first place?!

Looks like you want, directly:

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

rather than producing contentLists, aka x, and then y from it, no?




回答3:


You can achieve what you want in an efficient way like so:

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

In fact, f.read() first loads the whole file in memory, then .splitlines() creates a copy split into lines: there is not need for these two data structures, as you can simply read the file line by line and split each line in turn, as above. This is more efficient and simple.



来源:https://stackoverflow.com/questions/29002067/how-do-i-split-strings-within-nested-lists-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!