How can i make a 2D array with existing lists?

后端 未结 2 939
广开言路
广开言路 2021-01-28 11:47

for instance, i have a txt data called \'mazeline\' like this:

abcd
cdae
korp

So i first made 3 lists:

mazeline = readmaze.spli         


        
相关标签:
2条回答
  • 2021-01-28 12:44

    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()]
    
    0 讨论(0)
  • 2021-01-28 12:45

    Try this list comprehension:

    [[int(i) for i in line.strip()] for line in open('file/path')]
    
    0 讨论(0)
提交回复
热议问题