简体   繁体   中英

How do I split strings within nested lists in Python?

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?

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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