2

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
  • Side note: content = f.readlines() would be simpler and more efficient. There is an even simpler and more efficient solution, though (see my answer, for instance). Commented Mar 12, 2015 at 4:58

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.