2d array of lists in python

前端 未结 6 1346
北海茫月
北海茫月 2021-01-12 07:05

I am trying to create a 2d matrix so that each cell contains a list of strings. Matrix dimensions are known before the creation and I need to have access to any element from

6条回答
  •  感动是毒
    2021-01-12 07:42

    Here's some minimal example that extends 2d list of lists:

    my_list = [ [  [1] for x in range(4) ] for j in range(2) ]
    print('initial list is ', my_list)
    my_list[0][1].append(9)
    print('list after extension is ', my_list)
    

    and the results are

    initial list is  [[[1], [1], [1], [1]], [[1], [1], [1], [1]]]
    list after extension is  [[[1], [1, 9], [1], [1]], [[1], [1], [1], [1]]]
    

提交回复
热议问题