Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
59 views
in Technique[技术] by (71.8m points)

Python split function won't work on list to generate list of list

I am learning python and did the following experiement.

    text = "this is line one . this is line two . this is line three ."
    
    tokens = text.split(" ")            # split text into token with seperator "space"
    lioftokens = tokens.split(".")      # split tokens into list of tokens with seperator "dot"
    
    print(tokens)                       # output = ['this', 'is', 'line', 'one', '.', 'this', 'is', 'line', 'two', '.', 'this', 'is', 'line', 'three', '.']
    print(lioftokens)                   # expected output = [['this', 'is', 'line', 'one', '.'],
                                        #                    ['this', 'is', 'line', 'two', '.'],
                                        #                    ['this', 'is', 'line', 'three', '.']]

It gives error instead of expected output.

The split() is for string, not for list. How should I solve it?

#IamNewToPython

question from:https://stackoverflow.com/questions/65840547/python-split-function-wont-work-on-list-to-generate-list-of-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try using a list comprehension:

text = "this is line one . this is line two . this is line three ."
print([line.rstrip().split() for line in text.split('.') if line])

Output:

[['this', 'is', 'line', 'one'], ['this', 'is', 'line', 'two'], ['this', 'is', 'line', 'three']]

If you want to keep the splitters try:

import re
text = "this is line one . this is line two . this is line three ."
print([line.rstrip().split() for line in re.split('([^.]*.)', text) if line])

Output:

[['this', 'is', 'line', 'one', '.'], ['this', 'is', 'line', 'two', '.'], ['this', 'is', 'line', 'three', '.']]

Edit:

If you want to do list split try:

l = ['this', 'is', 'line', 'one', '.', 'this', 'is', 'line', 'two', '.', 'this', 'is', 'line', 'three', '.']
newl = [[]]
for i in l:
    newl[-1].append(i)
    if i == '.':
        newl.append([])
print(newl)

Output:

[['this', 'is', 'line', 'one', '.'], ['this', 'is', 'line', 'two', '.'], ['this', 'is', 'line', 'three', '.'], []]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...