How to nest splits in Python?

0

Following this question , I began to think, that it is a fairly common pattern, to interpret a string with "pairs" of values separated by some character, and how to solve it in Python ?. Let's see the example:

We have this string:

texto = "Respuesta1_10|Respuesta2_50|Respuesta4_90|Respuesta5_33" 

and we want to transform it into two list type variables in the following way:

respuestas = ["Respuesta1", "Respuesta2", "Respuesta3", "Respuesta5"]
status = [10, 50, 90, 33]

What shapes do we have in Python to achieve it?

    
asked by Patricio Moracho 07.09.2017 в 19:09
source

2 answers

1

As you ask "modes" to solve it, one of the most appropriate would be to use regular expressions , very similar to the java response you indicate.

Assuming that the text string has the suggested format, we can use a regular expression to cut it both by the character '_' and by '|' and do something like this:

import re

pattern = re.compile("_|[|]")

texto = "Respuesta1_10|Respuesta2_50|Respuesta4_90|Respuesta5_33" 
words = pattern.split(texto)

Now words is a list with the following content:

['Respuesta1',
 '10',
 'Respuesta2',
 '50',
 'Respuesta4',
 '90',
 'Respuesta5',
 '33']

To separate the two lists, just slice words :

respuestas = words[::2]
status = words[1::2]

You can also create something mixed between regular expressions and unzip . It is more complex, but it uses iterators, which makes it more efficient in the case of processing very long text strings:

import re

pat = re.compile('([^|]+)_([^|]+)')

texto = "Respuesta1_10|Respuesta2_50|Respuesta4_90|Respuesta5_33"

respuestas, status = zip(*(m.groups() for m in pat.finditer(texto)))
    
answered by 08.09.2017 / 02:02
source
1

The way I think is the most compact, I do not know if the most optimal is the following:

texto = "Respuesta1_10|Respuesta2_50|Respuesta4_90|Respuesta5_33"

Respuesta,Status = zip(*[e.split("_") for e in str.split("|")])

print(Respuesta)
print(Status)

Basically by understanding lists we put together the first split separating each pair of values through the pipe | and return in turn a new split , this time for the script under _ , which would put together a list like this:

[['Respuesta1', '10'], ['Respuesta2', '50'], ['Respuesta4', '90'], ['Respuesta5', '33']]

Finally, we make a unzip using zip(*[lista]) and we get our two variables with the corresponding tuples:

('Respuesta1', 'Respuesta2', 'Respuesta4', 'Respuesta5')
('10', '50', '90', '33')
    
answered by 07.09.2017 в 19:09