遍歷列表 # 直接遍歷list: # 通過索引獲取 # 基於enumerate()遍歷 # 基於iter()遍歷 遍歷dict # 通過key遍歷 # 直接遍歷,本質是先轉換成 (key,value)的list # iteritems() 返回值是 items的迭代器 ...
======================遍歷列表========================
# 直接遍歷list:
for elem in list: pass
# 通過索引獲取
for i in range(len(list)): elem = list[i] pass
# 基於enumerate()遍歷
for i, elem in enumerate(list): pass
# 基於iter()遍歷
listIter = iter(list) while True: try: elem = listIter.next() # .... expect StopIteration, ex: // NOOP
=====================遍歷dict==========================
# 通過key遍歷
for key in dict.keys(): value = dict.get(key) pass
# 直接遍歷,本質是先轉換成 (key,value)的list
for key,value in dict.items(): pass
# iteritems() 返回值是 items的迭代器
for key,value in dict.iteritems(): pass