Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

split python string without empty strings

The following code:

str = 'Welcome
to
PythonExamples
Welcome
to
PythonExamples'
chunks = str.split('
')
print(chunks)

Correctly prints out:

['Welcome', 'to', 'PythonExamples', 'Welcome', 'to', 'PythonExamples']

I want to split the string into strings that start with 'Welcome ' so I have tried the following:

str = 'Welcome
to
PythonExamples
Welcome
to
PythonExamples'
chunks = str.split('Welcome
')
print(chunks)

But this prints out:

['', 'to
PythonExamples
', 'to
PythonExamples']

Notice how the first entry is empty. How can I split it up correctly so that the output is?

['to
PythonExamples
', 'to
PythonExamples']
question from:https://stackoverflow.com/questions/66068367/split-python-string-without-empty-strings

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

1 Reply

0 votes
by (71.8m points)

If I understand correctly you want to avoid empty strings. You can just use list comprehension, do this:

chunks = [x for x in str.split('Welcome
') if x]

Should solve your problem. Why?

First of all, the list comprehension adds if x in the end, this means that it will include in the list only truthy values (or rather, will omit falsy values).

But why did you get '' in the first place? It would be the easier to point you at the source code for split:

while (maxcount-- > 0) {
    pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
    if (pos < 0)
        break;
    j = i + pos;
    SPLIT_ADD(str, i, j);
    i = j + sep_len;
}

Basically, split function looks for the next occurrence of sep in split(sep) and derives a substring from last occurrence to pos(it would do it maxcount times). Since you got Welcome in pos 0 and your "last occurence" is 0, it will make a substring from 0 to 0 which results in an empty string.

By the way, you would also get empty string for such string:

'Welcome Welcome to PythonExamples Welcome to PythonExamples'

results for your code, without my change:

['', '', 'to PythonExamples ', 'to PythonExamples']


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...