【2.3.1】python--字典

创建字典

>>> chesse={}
>>> chesse2=dict()
>>> type(chesse)
>>> type(chesse2)

字典的遍历

方法一:

>>> dict={'b': 'banana', 'o': 'orange', 'a': 'apple'}
>>> for (d,x) in dict.items():
         print("key:"+d+",value:"+str(x))

key:b,value:banana
key:o,value:orange
key:a,value:apple

>>> aDict = {'key1':'value1', 'key2':'value2', 'key3':'value3'}
>>> for (k,v) in aDict.items():
         print('%s:%s' %(k, v))

key1:value1
key2:value2
key3:value3

方法二:

>>> for k in d.keys():
...   if d[k] == 0:

方法三:(不是太建议)

>>> aDict = {'key1':'value1', 'key2':'value2', 'key3':'value3'}
>>> for d in aDict:
         print ("%s:%s" %(d, aDict[d]))



key1:value1
key2:value2
key3:value3
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn

Sam avatar
About Sam
专注生物信息 专注转化医学