Convert | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Convert

How to convert [‘2,1,3’, ‘3,1,3’, ‘2,9’] to [[2,1,3],[3,1,3],[2,9]] in python ?

27th Apr 2020, 11:40 AM
Casc
Casc - avatar
9 Answers
27th Apr 2020, 7:00 PM
visph
visph - avatar
+ 1
Please Show us your attempt..
27th Apr 2020, 11:47 AM
ANJALI SAHU
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
27th Apr 2020, 11:58 AM
Casc
Casc - avatar
0
the file is written like this: 2,1,3 3,1,3 2,9
27th Apr 2020, 12:00 PM
Casc
Casc - avatar
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!!
27th Apr 2020, 12:19 PM
ANJALI SAHU
0
it didnt work, but thank you for your help :)
27th Apr 2020, 12:30 PM
Casc
Casc - avatar
0
cassandratong but It works properly In my android
27th Apr 2020, 12:33 PM
ANJALI SAHU
29th Apr 2020, 4:51 AM
$layer
$layer - avatar
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
29th Apr 2020, 1:12 PM
Olaleye Moses Abiola