So I have a list of numerical values that are of type string. Some of the elements of the list contain more than one numerical value, for instance:
AC_temp= ['22', '45, 124, 12', '14', '12, 235']
I'm trying to turn each of these elements into integers, while still conserving the sub-lists/tuples, so I want it to look like:
AC_temp=[22, [45, 124, 12], 14, [12, 235]]
When I run the following:
for x in AC_temp:
if "," in x: #multiple values
val= x.split(",")
print(val)
I get the output I expect:
['187', '22']
['754', '17']
['417', '7']
['819', '13']
['606', '1']
['123', '513']
But then when I try to turn them into ints via the following:
for x in AC_temp:
if "," in x:
val= x.split(",")
for t in val:
AC.append(map(int, t))
else:
AC.append(map(int, x)
#print output#
for i in AC:
print(i)
It prints out the digits separately as such:
[1, 8, 7]
[2, 2]
[7, 5, 4]
[1, 7]
[4, 1, 7]
[7]
[8, 1, 9]
[1, 3]
[6, 0, 6]
[1]
[1, 2, 3]
[5, 1, 3]
What am I doing wrong?