python基础教程笔记(1)

遍历字典
1
2
3
d = {"x":1, "y":2, "z":3}
for key, value in d.items():
print key, "corresponds to ", value
并行迭代
1
2
3
4
5
6
7
8
names = ["ych", "cpp", "ws"]
ages = [12, 14, 54]

>>> zip(name, ages)
[("ych", 12 ), ("cpp", 14), ("ws", 54)]

for name, age in zip(names, ages):
print name, "is", age, "years old"
使用for循环同时迭代索引
1
2
for index, item in enumerate(some_list):
print "index: ", index, "item: ", item