python之字典

1.字典,列表都不能作为key,value值任意

2.字典是无序的

3.info={'k1':11,1:22,('abc',15):18}

   v1=info['k1']

   v2=info[1]

   v3=info[('abc',15)]

   print(v1,v2,v3)                //输出结果为11 22 18

4.①info={'k1':11,1:22,('abc',15):18}

      for item in info.keys():

           print(item,info[item])      //输出结果为k1            11
                                                                       1              (22, 55)
                                                                      ('abc', 15)  18

  ②info={'k1':11,1:22,('abc',15):18}

     for k,v in info.items():

             print(k,v)                       //字典可以进行for循环

5.info=dict.fromkeys({'k1',1,('abc',15)},123)

   print(info)                                //输出结果为{1: 123, 'k1': 123, ('abc', 15): 123},根据序列,创建字                                                            典,并统一值

6.info={'k1':11,1:22,('abc',15):18}

   v=info.get('k1111',666)

   print(v)                                    //输出结果为666,当找不到key:'k1111',返回666

7.info={'k1':11,1:22,('abc',15):18}

   v=info.pop('k1')

   print(info,v)                               //输出结果为{1: 22, ('abc', 15): 18} 11

8.info={'k1':11,1:22,('abc',15):18}

   k,v=info.popitem()

   print(info,k,v)                          //输出结果为{'k1': 11, 1: 22} ('abc', 15) 18,随机删

 

9.info={'k1':11,1:22,('abc',15):18}

   v=info.setdefault('k11',66)

   print(info,v)                            //输出结果为{'k1': 11, 1: 22, ('abc', 15): 18, 'k11': 66} 66,设置值,已                                                      经存在的,不设置,未存在的,设置进去

10.info={'k1':11,1:22,('abc',15):18}

     info.update({'k1':111,'k2':22)

 

     print(info)                          //输出结果为{'k1': 111, 1: 22, ('abc', 15): 18, 'k2': 22}

     info.update(k1=1111,k2='yyy') 

     print(info)                          //输出结果为{'k1': 1111, 1: 22, ('abc', 15): 18, 'k2':'yyy'}

11.dic={'one','two','three'}

     print(dict.fromkeys(dic,60))         //输出结果为{'one': 60, 'three': 60, 'two': 60}

     key=['one','two','three']

     value=[1,2,3]

     print(dict(zip(key,value)))            //输出结果为{'one': 1, 'two': 2, 'three': 3}

     dic=[('name','hg'),('age',20)]

     print(dict(dic))                              //输出结果为{'name': 'hg', 'age': 20}

     dic=dict(name='hgg',age=20)

     print(dic)                                       //输出结果为{'name': 'hgg', 'age': 20}

12.a=dict(name='hgg',age=20,num=[1,2,3])

     b=a.copy()

     a['you']='beautiful'

     print(a)                     //输出结果为{'name': 'hgg', 'age': 20, 'num': [1, 2, 3], 'you': 'beautiful'}

     print(b)                        //输出结果为{'name': 'hgg', 'age': 20, 'num': [1, 2, 3]}

     a['num'].remove(2)

     print(a)                        //输出结果为{'name': 'hgg', 'age': 20, 'num': [1, 3], 'you': 'beautiful'}

     print(b)                        //输出结果为{'name': 'hgg', 'age': 20, 'num': [1, 3]}

     

 

 

     

      

                                                  

   

 

posted @ 2020-01-18 21:57  戴猛有话说  阅读(151)  评论(0)    收藏  举报