简体   繁体   中英

Split string with 2 whitespace in between

For splitting words

str = "PYTHON IS VERY GOOD LANGUAGE CURRENTLY"
res = str.split(" ",2)
>> output: ["PYTHON", "IS", "VERY GOOD LANGUAGE CURRENTLY"]

But what if I was such output

output: ["PYTHON","IS VERY","GOOD LANGUAGE CURRENTLY"]

In python split() function you have 2 parameters, the separator and maxsplit . So if you specify a maxsplit then you will only split 2, the rest of the string won't be splitted.

You have said split function to split only two times

MAXSPLIT = 2
res = string.split(" ",MAXSPLIT) # Here, we are splitting 2 times.
# ['PYTHON', 'IS', 'VERY GOOD LANGUAGE CURRENTLY']

MAXSPLIT = 3 
res = string.split(" ",3) # Here, we are splitting 3 times.
# ['PYTHON', 'IS', 'VERY', 'GOOD LANGUAGE CURRENTLY']

From your example, it seems that you want to split your string to a list of substrings with 1 word, 2 word, 3 words, and so on. I don't think there is a builtin function for this. However, you can achieve it with the following code:

if __name__ == '__main__':
    str = "PYTHON IS VERY GOOD LANGUAGE CURRENTLY"
    splits = str.split(" ")  # Get string words
    res = []
    s = 0
    i = 0
    while s+1+i <= len(splits):
        res.append(" ".join(splits[s:s+1+i]))  # Make a string with i+1 words starting with word at index s
        # Update your values
        s+=i+1
        i+=1

    print(res)
['PYTHON', 'IS VERY', 'GOOD LANGUAGE CURRENTLY']

If, according to the question title and commenters understanding, your expected result is ["PYTHON IS", "VERY GOOD", " LANGUAGE CURRENTLY"] that is to say a list of 2-words-strings, it is even easier.

str = "PYTHON IS VERY GOOD LANGUAGE CURRENTLY"
    splits = str.split(" ")
    # Zip one every two words starting from the beginning
    # with one every two starting from second word
    # leads to a list [(word1, word2), (word3, word4), ...]
    # Join the tuples with a space, and build a list of these new strings
    res = [" ".join(x) for x in zip(splits[::2], splits[1::2])]
    print(res)
['PYTHON IS', 'VERY GOOD', 'LANGUAGE CURRENTLY']

This is a slightly complicated way of doing what you want and it produces a list of lists, but it shouldn't be too hard to extract your desired output. I'm sure someone knows how to do this in a more sensible way. I've only used str.split(" ") to create a list of the words in the original string.

import numpy as np


def factorial_recursive(n):
    if n == 0:
        return n
    elif n == 1:
        return n
    else:
        return n+factorial_recursive(n-1)


str = "PYTHON IS VERY GOOD LANGUAGE CURRENTLY"
str_lst = str.split(" ")

start = 0
end = 1
output_list = []
for i in range(int(np.floor(len(str_lst)/2))):
    if factorial_recursive(start) >= len(str_lst):
        break
    elif factorial_recursive(end) >= len(str_lst):
        break
    else:
        output_list.append(str_lst[factorial_recursive(start):factorial_recursive(end)])
        start += 1
        end += 1


print(output_list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM