在Python中有六种内建的序列:列表.元组.字符串.Unicode字符串.buffer对象和xrange对象.在这里暂时只讨论字符串.列表和元组的遍历. 一. 序列遍历 序列有两种遍历:一种通过值 另外一种通过索引 1.1 值遍历:s='abc'for x in s: print x z=('andy','leaf')for x in z: print x z={'tree','leaf'}for x in z: print x 1.2 索引遍历: l='abcd'for x in range…
字典遍历分为三种: 遍历key 遍历value 遍历key-value key遍历: >>> person={','city':'BeiJing'} >>> for key in person.keys(): print(key) name age city value遍历: >>> for value in person.values(): print(value) lizhong 26 BeiJing key-value遍历: >>&g…
(1)遍历key值 >>> a {'} >>> for key in a: print(key+':'+a[key]) a:1 b:2 c:3 >>> for key in a.keys(): print(key+':'+a[key]) a:1 b:2 c:3 在使用上,for key in a和 for key in a.keys():完全等价. (2)遍历value值 >>> for value in a.values()…
1.遍历字典 dict={'} for key in dict: print(key+':'+dict[key]) ssh://root@192.168.0.204:22/usr/bin/python -u /home/progect/app/py_code/test1.py a:1 b:2 c:3 Process finished with exit code 0…
#通过列表值,定义一个字典,来获取key和value str_list = [1,3,5,7,9,'i',9,'o',7,'i'] str_dict = {} for i in str_list: # 如果字典中包含i代表的key if i in str_dict: # 将i元素代表出现次数加1 str_dict[i] += 1 # 将ele元素代表出现次数设为1 else: str_dict[i] = 1 for i,value in str_dict.items(): print('%s的次…
dic1={"name":"kxb","age":28}for k,v in dic1.items(): print(k+",,,,"+str(v)) for v in dic1.values(): print("v...."+str(v)) for k in dic1.keys(): print("k...."+str(k))…
var dic={A:"AA",B:"BB",C:"CC"} //不能length去for循环(length:undefined) dic["D"]="DD"; //新增键值对 var tmp=dic["D"]; //通过key获取值 for (var key in dic) { var item = dic[key]; console.log(item); //AA,BB,CC,DD…