Splitting a string into a list (but not splitting adjacent numbers) in Python

For example, I have:

string = "123ab4 5" 

I want to get the following list:

 ["123","ab","4","5"] 

and not a list (string) giving me:

 ["1","2","3","a","b","4"," ","5"] 
+4
source share
5 answers

Find one or more adjacent digits ( \d+ ), or if you cannot find non-digit characters without spaces ( [^\d\s]+ ).

 >>> string = '123ab4 5' >>> import re >>> re.findall('\d+|[^\d\s]+', string) ['123', 'ab', '4', '5'] 

If you do not want the letters to join together, try the following:

 >>> re.findall('\d+|\S', string) ['123', 'a', 'b', '4', '5'] 
+8
source

Other solutions are certainly simpler. If you want something much less straightforward, you can try something like this:

 >>> import string >>> from itertools import groupby >>> s = "123ab4 5" >>> result = [''.join(list(v)) for _, v in groupby(s, key=lambda x: x.isdigit())] >>> result = [x for x in result if x not in string.whitespace] >>> result ['123', 'ab', '4', '5'] 
+1
source

You can do:

 >>> [el for el in re.split('(\d+)', string) if el.strip()] ['123', 'ab', '4', '5'] 
+1
source

you can do a few things here you can

1. iterate over the list and make groups of numbers as they become available, adding them to the list of results.

not a great solution.

2. Use regular expressions.

implementation 2:

 >>> import re >>> s = "123ab4 5" >>> re.findall('\d+|[^\d]', s) ['123', 'a', 'b', '4', ' ', '5'] 

you want to capture any group that is at least 1 number \d+ or any other character.

change

At first John beat me up to the right decision. and this is a wonderful solution.

I will leave it here because someone else may misunderstand the question and look for the answer to what I thought was written. I was impressed that the OP wanted to capture only groups of numbers and leave everything else individual.

0
source

This will give you a split:

 re.findall(r'\d+|[a-zA-Z]+', "123ab4 5") ['123', 'ab', '4', '5'] 
0
source

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


All Articles