python:遍历多维数组

1. 同等长度的二维列表的遍历

a = [[1,2],[1,2]]
for i,j in a :
    print(i,j,';',end="")
# >>>1 2 ;1 2 ;

2. 同等长度的三维列表的遍历

a = [[1,2,3],[1,2,3],[1,2,3]]
for i,j,k in a :
    print(i,j,k,';',end="")
# >>>1 2 3 ;1 2 3 ;1 2 3 ;

3. 同等长度的四维列表的遍历

a = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
for i,j,k,l in a :
    print(i,j,k,l,';',end="")
# >>>1 2 3 4 ;1 2 3 4 ;1 2 3 4 ;1 2 3 4 ;

4. 同理应该可以遍历出所有元素

5.多维不定长元素的遍历

a = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
yuansu = []
def bianli(lists):
    for i in lists:
        if type(i) != list:
            yuansu.append(i)
        else:
            bianli(i)
bianli(a)
print(yuansu)
# >>>[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]

你可能感兴趣的:(Python)