Python列表遍历方法

Python列表遍历方法

1、for…in…循环

for 变量 in 列表:
        使用变量的操作

2、while循环

i = 0
    while i<len(列表):
        使用列表中的元素(列表[i])的操作
        i += 1

示例:

 
list = [1, 2, 3]
for id in list:
    print id

i = 0
while i<len(list):
	print(list[i])
	i += 1

列表中含有字符串时比较有效的遍历方法

list = ["aaa", "bbb", "ccc"]
for i in range(len(list)):
	print(i, list[i])

猜你喜欢

转载自blog.csdn.net/m0_47665468/article/details/113915016
今日推荐