# 遍历列表
ls = [1, 2, 3, 4]
for l in ls:
print(l)
>>>1
>>>2
>>>3
>>>4
for i in range(len(ls)):
print(ls[i])
>>>1
>>>2
>>>3
>>>4
for index, value in enumerate(ls):
print(index, value)
>>>0 1
>>>1 2
>>>2 3
>>>3 4
# 遍历元祖
tup = (1, 2, 3, 4)
for t in tup:
print(t)
>>>1
>>>2
>>>3
>>>4
for i in range(len(tup)):
print(tup[i])
>>>1
>>>2
>>>3
>>>4
for index, value in enumerate(tup):
print(index, value)
>>>0 1
>>>1 2
>>>2 3
>>>3 4
# 遍历字典
dic = {'one': 1, 'two': 2, 'three': 3}
for k in dic.keys():
print(k, dic.get(k))
>>> one 1
>>> two 2
>>> three 3
for k, v in dic.items():
print(k, v)
>>> one 1
>>> two 2
>>> three 3
for k in dic:
print(k, dic.get(k))
>>> one 1
>>> two 2
>>> three 3
python初入江湖:遍历
最新推荐文章于 2024-07-31 16:08:35 发布