python中的可迭代對象有列表、字符串、元組、字典幾種
1.列表
L = list(range(100))for i in L:
print(i)
2.字符串
S = 'Say YOLO Again!'for s in S:
print(s)
返回'索引-元素'對:
for i, value in enumerate('Say YOLO Again.'):
print(i, value)
3.元組
T = tuple(range(100))for i in T:
print(i)
4.字典
dic = {'name': 'chen', 'age': 25, 'loc': 'Tianjin'}
# 以列表的形式返回
keylist(dic.keys())
# 以列表的形式返回
valuelist(dic.values())
# 循環(huán)key
for key in dic:
print(key)
# 循環(huán)value
for value in dic.values():
print(value)
# 循環(huán)key, value
for key, value in dic.items():
print(key, value)