当前位置:   article > 正文

python如何遍历二维数组的列元素_Python——二维数组遍历操作

python遍历二维列表

一、遍历数组(操作Value)

1.使用二维列表遍历二维数组

python 创建List二维列表

lists = [[] for i in range(3)] # 创建的是多行三列的二维列表

for i in range(3):

lists[0].append(i)

for i in range(5):

lists[1].append(i)

for i in range(7):

lists[2].append(i)

print("lists is:", lists)

# lists is: [[0, 1, 2], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6]]

输出数组行和列数

# 输出数组的行和列数

arr = [[1,4,7,10,15], [2,5,8,12,19], [3,6,9,16,22], [10,13,14,17,24]]

print arr.shape # (4, 5)

# 只输出行数

print arr.shape[0] # 4

# 只输出列数

print arr.shape[1] # 5

或者:

In [48]: arr = [[1,4,7,10,15], [2,5,8,12,19], [3,6,9,16,22], [10,13,14,17,24]]

In [49]: len(arr) #行数

Out[49]: 4

In [50]: len(arr[0]) #列数

Out[50]: 5

使用二维列表索引遍历二维列表

注意python中二维列表和matlab以及C和JAVA中一样,不需要每行中列的数量相

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/201759
推荐阅读
相关标签
  

闽ICP备14008679号