Reputation: 3223
for instance, i have a txt data called 'mazeline' like this:
abcd
cdae
korp
So i first made 3 lists:
mazeline = readmaze.split()
mline0 = list(mazeline[0])
mline1 = list(mazeline[1])
mline2 = list(mazeline[2])
So the 3 lists are:
mline0 = [a,b,c,d]
mline1 = [c,d,a,e]
mline2 = [k,o,r,p]
and i want to make a 2D array like this:
[[a,b,c,d],[c,d,a,e],[k,o,r,p]]
or is there any way that i can make a 2d array directly from the first data?
any suggestions? any help would be good.
Upvotes: 0
Views: 408
Reputation: 113915
Try this list comprehension:
[[int(i) for i in line.strip()] for line in open('file/path')]
Upvotes: 0
Reputation: 304137
Just put the lists inside another list
res = [mline0, mline1, mline2]
more simply, you can skip the intermediate variables and use a list comprehension
res = [list(mline) for mline in readmaze.split()]
Upvotes: 2