Skip to content Skip to sidebar Skip to footer

How Can I Make A 2d Array With Existing Lists?

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(mazelin

Solution 1:

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()]

Solution 2:

Try this list comprehension:

[[int(i) for i in line.strip()] for line in open('file/path')]

Post a Comment for "How Can I Make A 2d Array With Existing Lists?"