0
Convert
How to convert [‘2,1,3’, ‘3,1,3’, ‘2,9’] to [[2,1,3],[3,1,3],[2,9]] in python ?
9 Respuestas
+ 1
Please Show us your attempt..
0
def nested_int_list_from_file(file):
f = open(file)
read = f.read()
lines = lines.strip(‘\n’)
line = lines.split(‘\n’)
return line
- i got [‘2,1,3’, ‘3,1,3’, ‘2,9’]
then i tried adding:
for i in line:
new_line = int(i)
return new_line
but it wont convert to integers
0
the file is written like this:
2,1,3
3,1,3
2,9
0
s=eval(input("Enter list:"))
final,l=[],[]
for i in s:
a=i.split(",")
for j in a:
l.append(int(j))
final.append(l)
print(final)
I hope this helps!!
0
it didnt work, but thank you for your help :)
0
cassandratong but It works properly In my android
0
The code is as follow :
https://code.sololearn.com/cP4Ipx6eGLue/?ref=app
0
The crudest logical approach is to do something like:
a = ['2,1,3', '3,1,3', '2,9'] # to [[2,1,3], [3,1,3], [2,9]]
b = len(a)
c = []
d = []
e = []
f = []
c.append(a[0])
d.append(a[1])
e.append(a[2])
f = [c, d, e]
print(f)
This will give you the result. Hope it gives you an idea of what you want to achieve