I want to turn this lst = ['123','456','789']
into this lst = [[1,2,3],[4,5,6],[7,8,9]]
I tried :
for i in range(len(lst)):
lst[i] = list(lst[i])
this produced lst = [['1','2','3'],['4','5','6'],['7','8','9']]
And I don't know how to turn those string numbers into integers. How do I do it?
note - I can't use map
.
CodePudding user response:
List comprehension approach:
lst = ['123', '456', '789']
lst = [[int(i) for i in string_number] for string_number in lst]
CodePudding user response:
Here is the solution:
lst = [['123'],['456'],['789']]
ans = []
temp = []
for i in lst:
for j in i:
temp = list(j)
for k in range(len(temp)):
temp[k] = int(temp[k])
ans.append(temp)
print(ans)
# output - [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
CodePudding user response:
well here is a simple solution for your problem:
final_list=[]
for i in lst:
tmp_list=[]
text=str(i[0])
for c in text:
tmp_list.append(int(c))
final_list.append(tmp_list)
CodePudding user response:
lst = ['123','456','789']
for i in range(len(lst)):
lst[i] = [int(x) for x in lst[i]]