Two dimensional array in python

后端 未结 10 990
旧巷少年郎
旧巷少年郎 2020-12-02 07:49

I want to know how to declare a two dimensional array in Python.

arr = [[]]

arr[0].append(\"aa1\")
arr[0].append(\"aa2\")
arr[1].append(\"bb1\")
arr[1].appe         


        
10条回答
  •  感动是毒
    2020-12-02 08:43

    When constructing multi-dimensional lists in Python I usually use something similar to ThiefMaster's solution, but rather than appending items to index 0, then appending items to index 1, etc., I always use index -1 which is automatically the index of the last item in the array.

    i.e.

    arr = []
    
    arr.append([])
    arr[-1].append("aa1")
    arr[-1].append("aa2")
    
    arr.append([])
    arr[-1].append("bb1")
    arr[-1].append("bb2")
    arr[-1].append("bb3")
    

    will produce the 2D-array (actually a list of lists) you're after.

提交回复
热议问题