字典(dict)常用方法简介及相关练习

字典,是用来存储键值对的类型

import collections;
dic={"k1":"v1","k2":"v2"}
print("取出字典所有的键:")
for k in dic.keys():
    print(k)
print("取出字典所有的值:")
for i in dic.values():
    print(i)
print("取出字典的键值对:")
for k,i in dic.items():
    print(k,i)
print(dic["k1"])
print(dic["k2"])
# print(dic["k3"])       #不存在报错
print(dic.get("k1"))
print(dic.get("k2"))
# print(dic.get("k3"))      #不存在返回none
print("若不存在k,则返回默认值:",dic.get("k4",'eric'))
print(dic.items())
dic1=dic.pop("k1")
print("字典无序,删除某一值必须指定键值:",dic)
print("字典删除的值为:",dic1)
dic.update({"k4":9999})
print("更新后的字典为:",dic)

print("********************Python计数器**********************")
print("计数器继承字典,字典的方法在计数器中均适用")
cc="aasnsnssdaadsadsdsndsmdsm"
c=collections.Counter(cc)
print("Python计数器(并非纯粹的字典),计算各元素出现的次数:",c)
ret=c.most_common(2)
print("拿到前两位:",ret)
print("取出counter中的所有元素:")
for  i in c.items():
    print(i)

 Counter计数器的相关操作,使用该计数器以及下面所介绍的几种方法均需导入collections

                                                                                      
ll=[11,22,33,22,11,33,55]                                                             
lc=collections.Counter(ll)                                                            
print(lc)                                                                             
#更新将新列表添加到lc中                                                                         
lc.update([66,33])                                                                    
print(lc)                                                                             
#相减  lc减去后面的列表                                                                        
lc.subtract([66,33])                                                                  
print(lc)                                                                             
                                                                                      

orderedDict有序字典,因为字典内容和set集合一样,是无序的,为了方便字典内部的排序

name="有序字典"                                                                           
print(name.center(60,"*"))                                                            
dic2=collections.OrderedDict()                                                        
dic2["k1"]="v1"                                                                       
dic2["k2"]="v2"                                                                       
dic2["k3"]="v3"                                                                       
print("输出有序的字典:",dic2)                                                                
# dic2.move_to_end("k1")                                                              
# print("将k1移至最后一个:",dic2)                                                            
# dic3=dic2.pop("k2")                                                                 
# print("删除元素k2:",dic2)                                                               
#                                                                                     
# dic3=dic2.popitem()                                                                 
# print("删除的元素为:",dic3)                                                               
# print("从后往前删除:",dic2)                                                               
# dic2["k4"]=None                                                                     
                                                                                      
#设置默认值                                                                                
dic2.setdefault("k4","v5")                                                            
print(dic2)                                                                           
                                                                                      
dic2.update({"k5":"v6","k6":"v7"})                                                    
print(dic2)                                                                           

defaultDict默认字典类型,指出默认类型,即可使用该类型相关的方法

name1="默认类型字典"                                       
print(name1.center(60,"*"))                          
# dic={}                                             
# dic["k1"].append("alex")  #该语句执行报错                 
                                                     
# 指定默认字典类型,即可使用该类型的相关方法                              
dic3=collections.defaultdict(list)                   
dic3["k1"].append("alex")                            
print(dic3)                                          
                                                     
#使用该方法处理之前作业更加简单                                     
values=[11,22,33,44,55,66,77,88,99]                  
my_dic=collections.defaultdict(list)                 
                                                     
for value in values:                                 
    if value>66:                                     
        my_dic["k1"].append(value)                   
    else:                                            
        my_dic["k2"].append(value)                   
                                                     
print(my_dic)                                        

 

posted @ 2016-10-09 19:24  Eric9  阅读(1020)  评论(0编辑  收藏  举报