python 循环遍历笔记

python 的循环遍历功能比较特别, 和传统的C++ , JAVA , C# 均存在一定差异

for i in range(0,10):    // python
    print 1

for(int i =0; i<10: i++){  c++
    cout << i <

由于range() 并不能提供有效的index , 在处理list 或者map等结构体需要定位index的时候, 甚是不便
但是python 也提供相应的解决方案

list 遍历获取index

i_list = [0,1,2,3,4,5,6,7,8,9]
for index , item in enumerate(i_list):
    print index , item

dict 遍历获取key 和value

i_dict = {1:1,2:2,3:3}
for key , value in  i_dict.iteritems():
    print key ,value

你可能感兴趣的:(C++,python)