python日记(四)字典的常见用法

#author:ne_zha
#dictionary

            feng_shen_bang ={
                1:'哪吒',
                2:'雷震子',
                3:'杨戬'
            
            }
            print(feng_shen_bang[1])
            print(feng_shen_bang)
            
            #dictionary的三种删除方式:
            #feng_shen_bang.clear()
            print(feng_shen_bang.pop(2))
            print(feng_shen_bang.popitem())
            print(feng_shen_bang)
            
            
            feng_shen_bang ={
                1:'哪吒',
                2:'雷震子',
                3:'杨戬'
            }
            #合并,更新
            xi_rou_ji ={
                4:'孙悟空',
                5:'猪八戒'
            }
            
            feng_shen_bang.update(xi_rou_ji)
            print(feng_shen_bang)
            
            
            print(feng_shen_bang.items())
            #返回字典中的所有值
            print(feng_shen_bang.values())
            #返回字典中的所有键
            print(feng_shen_bang.keys())
            
            
            #fromkeys用法:
            seq = ('name', 'age', 'sex')
            dict = dict.fromkeys(seq)
            print("New Dictionary : %s" %  str(dict))
            dict = dict.fromkeys(seq, 10)
            print("New Dictionary : %s" %  str(dict))
            
            dict['name'] ="ne_zha"
            print(dict)
            
            c = dict.fromkeys([6,7,8],[1,{"name":"alex"},444])
            print(c)
            c[7][0]=2
            print(c)
            c[7][1]['name'] = "Jack Chen"
            print(c)
原文地址:https://www.cnblogs.com/ne-zha/p/7160632.html