注册 登录
编程论坛 Python论坛

关于list中len()函数

sora2015x 发布于 2020-02-18 23:19, 2160 次点击
大家好!有个问题想请教大家,问题如下:
若 vehicle = [ ['train', 'car'], ['bus', 'subway'], ['ship', 'bicycle'],['car'] ], 则 len(vehicle[1][0])结果是
什么?
A. 3
B. 7
C. 6
D. 4

答案是: A

其中 len(vehicle[1][0])弄不明白,请大神详细讲解!谢谢!
1 回复
#2
Samson幽客2020-02-20 14:55
vehicle是个二维数组,数组计数是从0开始的,所以vehicle的首地址应为vehicle[0][0]
具体样式(只是打个比方,这样看起来容易理解些):
       0      1
    ————————
0  | train  car     
1  | bus    subway  
2  | ship   bicycle
3  | car    '\n'   
len()是计算数组长度的函数,题目中问的是len(vehicle[1][0])此时是从第二行“1”开始计数,因此答案为三。
1