Hello, I need help to write a Python code in order to from this input (a string, len (data) = 5, len (data[0])=63):

["['1426', '1095', '1094', '1093', '1092', '974', '869', '1572']\n", "['1495', '1305', '1264', '1227', '1215', '1142', '1141', '1115']\n", "['1001', '997', '927', '904', '871', '776', '712', '612']\n", "['567', '538', '429', '415', '332', '326', '250', '247']\n", "['246', '245', '244', '155', '145', '133', '101']\n"]


to this output "data" (a list of int, len (data) = 5, len (data[0])=8):

[['1426', '1095', '1094', '1093', '1092', '974', '869', '1572'], ['1495', '1305', '1264', '1227', '1215', '1142', '1115'], ['1001', '997', '904', '871', '776', '712', '612'], ['567', '538', '429', '415', '332', '326'], ['246', '245', '244', '155', '145', '133', '101']]


Thanks for your help!!

Here is one solution ...

list1 = [
"['1426', '1095', '1094', '1093', '1092', '974', '869', '1572']\n",
"['1495', '1305', '1264', '1227', '1215', '1142', '1141', '1115']\n", 
"['1001', '997', '927', '904', '871', '776', '712', '612']\n", 
"['567', '538', '429', '415', '332', '326', '250', '247']\n", 
"['246', '245', '244', '155', '145', '133', '101']\n"]

list2 = []
for line in list1:
    # strip off the trailing '\n'
    line = line.rstrip()
    # use eval() to turn string line into list
    list2.append(eval(line))

import pprint
pprint.pprint(list2)

"""result >>>
[['1426', '1095', '1094', '1093', '1092', '974', '869', '1572'],
 ['1495', '1305', '1264', '1227', '1215', '1142', '1141', '1115'],
 ['1001', '997', '927', '904', '871', '776', '712', '612'],
 ['567', '538', '429', '415', '332', '326', '250', '247'],
 ['246', '245', '244', '155', '145', '133', '101']]

"""
Comments
Right the solution I was looking for.

Here is one solution ...

list1 = [
"['1426', '1095', '1094', '1093', '1092', '974', '869', '1572']\n",
"['1495', '1305', '1264', '1227', '1215', '1142', '1141', '1115']\n", 
"['1001', '997', '927', '904', '871', '776', '712', '612']\n", 
"['567', '538', '429', '415', '332', '326', '250', '247']\n", 
"['246', '245', '244', '155', '145', '133', '101']\n"]

list2 = []
for line in list1:
    # strip off the trailing '\n'
    line = line.rstrip()
    # use eval() to turn string line into list
    list2.append(eval(line))

import pprint
pprint.pprint(list2)

"""result >>>
[['1426', '1095', '1094', '1093', '1092', '974', '869', '1572'],
 ['1495', '1305', '1264', '1227', '1215', '1142', '1141', '1115'],
 ['1001', '997', '927', '904', '871', '776', '712', '612'],
 ['567', '538', '429', '415', '332', '326', '250', '247'],
 ['246', '245', '244', '155', '145', '133', '101']]

"""

Thanks, very much. It works as "stand alone"; embedded in my code it gives me back the following error message:

list2.append(eval(line))
File "<string>", line 1
[
^
SyntaxError: unexpected EOF while parsing
What am I doing wrong?

my list1 is:
>>> list1
"\n\n\n\n\n"
>>> len (list1)
293
>>> print list1


Thanks

Here is one solution ...

list1 = [
"['1426', '1095', '1094', '1093', '1092', '974', '869', '1572']\n",
"['1495', '1305', '1264', '1227', '1215', '1142', '1141', '1115']\n", 
"['1001', '997', '927', '904', '871', '776', '712', '612']\n", 
"['567', '538', '429', '415', '332', '326', '250', '247']\n", 
"['246', '245', '244', '155', '145', '133', '101']\n"]

list2 = []
for line in list1:
    # strip off the trailing '\n'
    line = line.rstrip()
    # use eval() to turn string line into list
    list2.append(eval(line))

import pprint
pprint.pprint(list2)

"""result >>>
[['1426', '1095', '1094', '1093', '1092', '974', '869', '1572'],
 ['1495', '1305', '1264', '1227', '1215', '1142', '1141', '1115'],
 ['1001', '997', '927', '904', '871', '776', '712', '612'],
 ['567', '538', '429', '415', '332', '326', '250', '247'],
 ['246', '245', '244', '155', '145', '133', '101']]

"""

It works perfectly!!! My bad. I used one time (the second time) read, and I got the error and len(list)=259; and the second time I used readlines and it worked perfectly.

Thanks very much

list of int

list3 = [[int(x) for x in y] for y in list2]

Thanks a lot. Very useful!

Here is one solution ...

list1 = [
"['1426', '1095', '1094', '1093', '1092', '974', '869', '1572']\n",
"['1495', '1305', '1264', '1227', '1215', '1142', '1141', '1115']\n", 
"['1001', '997', '927', '904', '871', '776', '712', '612']\n", 
"['567', '538', '429', '415', '332', '326', '250', '247']\n", 
"['246', '245', '244', '155', '145', '133', '101']\n"]

list2 = []
for line in list1:
    # strip off the trailing '\n'
    line = line.rstrip()
    # use eval() to turn string line into list
    list2.append(eval(line))

import pprint
pprint.pprint(list2)

"""result >>>
[['1426', '1095', '1094', '1093', '1092', '974', '869', '1572'],
 ['1495', '1305', '1264', '1227', '1215', '1142', '1141', '1115'],
 ['1001', '997', '927', '904', '871', '776', '712', '612'],
 ['567', '538', '429', '415', '332', '326', '250', '247'],
 ['246', '245', '244', '155', '145', '133', '101']]

"""

The second style is then to make a list comprehension. Sometimes it feels better or more 'pythonic'. Take your pick!

If you have not been learning other languages, it is maybe good to learn this style right away. List comprehensions are faster than loop with append, so for big data they are good choice. General principle is though to get the progrma working first and after to optimize if needed. Sometimes when you have learned the longer style, you need to do the first version first and turn fors in opposite order:

import pprint
list1 = [
"['1426', '1095', '1094', '1093', '1092', '974', '869', '1572']\n",
"['1495', '1305', '1264', '1227', '1215', '1142', '1141', '1115']\n",
"['1001', '997', '927', '904', '871', '776', '712', '612']\n",
"['567', '538', '429', '415', '332', '326', '250', '247']\n",
"['246', '245', '244', '155', '145', '133', '101']\n"]

# strip off the trailing '\n' by .rstrip()
# use eval() to turn string line into list
list2= [eval(line.rstrip()) for line in list1]
list3= [[int(number) for number in stringform]
        for stringform in list2]

print('As strings')
pprint.pprint(list2)
print('As numbers')
pprint.pprint(list3)

"""result >>>
As strings
[['1426', '1095', '1094', '1093', '1092', '974', '869', '1572'],
 ['1495', '1305', '1264', '1227', '1215', '1142', '1141', '1115'],
 ['1001', '997', '927', '904', '871', '776', '712', '612'],
 ['567', '538', '429', '415', '332', '326', '250', '247'],
 ['246', '245', '244', '155', '145', '133', '101']]
As numbers
[[1426, 1095, 1094, 1093, 1092, 974, 869, 1572],
 [1495, 1305, 1264, 1227, 1215, 1142, 1141, 1115],
 [1001, 997, 927, 904, 871, 776, 712, 612],
 [567, 538, 429, 415, 332, 326, 250, 247],
 [246, 245, 244, 155, 145, 133, 101]]
"""
This question has already been answered. Start a new discussion instead.