Python splits a string exactly in one space. if double space makes the word "not a word",

I have the following line.

words = "this is a book and i like it"

What I want is that when I split it into one space, I get the following. wordList = words.split(" ") print wordList << ['this','is','a',' book','and','i',' like','it']

A simple function words.split(" ")splits the string, but with double space, it removes both spaces, which gives 'book'and 'like'. and I need to ' book', and ' like'kept the extra spaces in the output split in the case of double, triple ... n spaces

+4
source share
4 answers

: char, \s? , + + .

: rstrip , , .

import re
words = "this is a  book and i  like it"
print(re.findall(r'\s?(\s*\S+)', words.rstrip()))
# => ['this', 'is', 'a', ' book', 'and', 'i', ' like', 'it']

. Python. re.findall , , .

, ​​- regex. :

  • \s? - 1 0 (- ? quantifier) ​​
  • (\s*\S+) - # 1
    • \s* - (- *)
    • \S+ - 1 (- + quantifier) ​​ .
+3

, , (? < =) :

import re

re.split("(?<=\\S) ", words)
# ['this', 'is', 'a', ' book', 'and', 'i', ' like', 'it']

:

re.split("(?<!\\s) ", words)
# ['this', 'is', 'a', ' book', 'and', 'i', ' like', 'it']
+5

If you do not want to use a regular expression and want to keep something close to your own code, you can use something like this:

words = "this is a  book and i  like it"
wordList = words.split(" ")
for i in range(len(wordList)):
    if(wordList[i]==''):
        wordList[i+1] = ' ' + wordList[i+1]
wordList = [x for x in wordList if x != '']  
print wordList
# Outputs: ['this', 'is', 'a', ' book', 'and', 'i', ' like', 'it']
0
source

List comprehension alternative:

word_list = iter(words.split(" "))
["".join([" ", next(word_list)]) if not w else w for w in word_list] 
# ['this', 'is', 'a', ' book', 'and', 'i', ' like', 'it']
0
source

Source: https://habr.com/ru/post/1676912/


All Articles