Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

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?

Solution 2:

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.

Solution 3:

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

withopen(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.

Post a Comment for "How Do I Split Strings Within Nested Lists In Python?"