Home > Software engineering >  How do I collect values into a list in Python standard regex?
How do I collect values into a list in Python standard regex?

Time:12-20

I have a string with repeated parts:

s = '[1][2][5] and [3][8]'

And I want to group the numbers into two lists using re.match. The expected result is:

{'x': ['1', '2', '5'], 'y': ['3', '8']}

I tried this expression that gives a wrong result:

re.match(r'^(?:\[(?P<x>\d )\])  and (?:\[(?P<y>\d )\]) $', s).groupdict()
# {'x': '5', 'y': '8'}

It looks like re.match keeps the last match only. How do I collect all the parts into a list instead of the last one only?

Of course, I know that I could split the line on ' and ' separator and use re.findall for the parts instead, but this approach is not general enough because it gives some issues for more complex strings so I would always need to think about correct splitting separately all the time.

CodePudding user response:

We can use regular expressions here. First, iterate the input string looking for matches of the type [3][8]. For each match, use re.findall to generate a list of number strings. Then, add a key whose value is that list. Note that we maintain a list of keys and pop each one when we use it.

import re

s = '[1][2][5] and [3][8]'
keys= ['x', 'y']
d = {}
for m in re.finditer('(?:\[\d \]) ', s):
    d[keys.pop(0)] = re.findall(r'\d ', m.group())

print(d)  # {'y': ['3', '8'], 'x': ['1', '2', '5']}

CodePudding user response:

import re

s = '[1][2][5] and [3][8]'

# Use a regular expression to extract the numbers from the string
numbers = re.findall(r'\d ', s)

# Group the numbers into a dictionary using a dictionary comprehension
result = {
    'x': numbers[:3],  # First three numbers
    'y': numbers[3:]   # Remaining numbers
}

print(result)  # {'x': ['1', '2', '5'], 'y': ['3', '8']}

The regular expression \d matches one or more digits, and the findall() function returns a list of all the matches. The dictionary comprehension then groups the numbers into the desired lists x and y.

  • Related