Turning a numeric string into a list of numbers in python

-2

I need to turn an entry into a list.

The entry has the following form:

['[11, 14]', '[8, 17, 18, 24, 29, 37, 44, 49, 51, 55, 62, 63, 64, 76, 82, 84, 93, 97]']

is read from a csv file. Note that it is composed of two strings , the first '[11, 14]' ; and the second '[8, 17, 18, 24, 29, 37, 44, 49, 51, 55, 62, 63, 64, 76, 82, 84, 93, 97]' .

The output I need is a list of integers:

[11, 14, 8, 17, 18, 24, 29, 37, 44, 49, 51, 55, 62, 63, 64, 76, 82, 84, 93, 97]

I looked here in the forum and google, even if I found some similar tips, I did not succeed.

    
asked by anonymous 03.09.2018 / 02:31

4 answers

4

You can work with JSON, since the values you have are sequences in JSON as well.

>>> json.loads('[1, 2, 3]')  # [1, 2, 3]

And, to merge the lists, you can use the itertools.chain.from_iterable :

>>> itertools.chain([1, 2, 3], [4, 5, 6])  # [1, 2, 3, 4, 5, 6]

So, getting:

import json
import itertools

linha = ['[11, 14]', '[8, 17, 18, 24, 29, 37, 44, 49, 51, 55, 62, 63, 64, 76, 82, 84, 93, 97]']
resultado = list(itertools.chain.from_iterable(json.loads(item) for item in linha))

print(resultado)

See working at Repl.it

    
03.09.2018 / 13:59
2
entrada = ['[11, 14]', '[8, 17, 18, 24, 29, 37, 44, 49, 51, 55, 62, 63, 64, 76, 82, 84, 93, 97]']

saida = []
for string in entrada:
    #tiro o '[' e o ']' e depois separo pelas virgulas. Isso me retorna uma lista com strings
    lista_strings = string.replace('[','').replace(']','').split(',')
    #Transformo as strings da lista em inteiros
    lista_inteiros = [int(i) for i in lista_strings]
    saida.append(lista_inteiros)

>>> print(saida)
[[11, 14], [8, 17, 18, 24, 29, 37, 44, 49, 51, 55, 62, 63, 64, 76, 82, 84, 93, 97]]
    
03.09.2018 / 03:04
0

Well, using the channel was not just waiting for help, I've been searching since the moment of the post and I was able to solve it like this:

import re

lista = ['[11, 14]', '[8, 17, 18, 24, 29, 37, 44, 49, 51, 55, 62, 63, 64, 76, 82, 84, 93, 97]']

listalimpa = []

for dado in lista:
    #Divido as string '[11, 14]' e '[8, 17, 18, 24, 29, 37, 44, 49, 51, 55, 62, 63, 64, 76, 82, 84, 93, 97]' faço uma divisão 
    for x in dado.split(','):
    #aqui substituo os caracteres indesejados por nada usando o método sub, do módulo re
        listalimpa.append(int(re.sub('\[|\]|,|''\s*', "", x)))
print(listalimpa)

The output was as expected:

[11, 14, 8, 17, 18, 24, 29, 37, 44, 49, 51, 55, 62, 63, 64, 76, 82, 84, 93, 97]

Well, as I mentioned in the original post, the answer was not one of the hardest.

    
03.09.2018 / 14:47
0
a = ['[11, 14]', '[8, 17, 18, 24, 29, 37, 44, 49, 51, 55, 82, 84, 93, 97]']

Using lists in understanding we can:

b=[eval(x) for x in a]   ## [[11, 14], [8, 17, 18, 2...]]

To get the single list we have several assumptions:

c=sum(b,[])

or

c=[y for x in a for y in eval(x)]

( eval(...) can raise security issues if for example   a is being read interactively. For uncontrolled entries, use ast.literal_eval() instead of eval )

    
03.09.2018 / 14:06