Python数据类型之dict相关常用操作

info = {

    'stu1101': "TengLan Wu",

    'stu1102': "LongZe Luola",

    'stu1103': "XiaoZe Maliya",

}

 

#取值

print(info["stu1101"])    #取出按键的值

 

#修改

info["stu1101"] = "刀前卫"  #修改key的值

info["stu1104"] = "changjinkong" #修key的值 如果没有key就自动添加key

 

#删除

#del info["stu1101"] #删除改key(值自然就没了)

info.pop("stu1101") #删除该key 和上面效果一样

print(info)

 

#查找

#info['stu1104'] #用该方式查找,如果没有该key就会报错,所以不用

print(info.get('stu1103')) #用.get('xxx')查找是最有用的

print(info.get('stu1105'))  #如果没有该key直接回复none而不是报错

 

#判断key会否存在

print('stu1103'in info) #判断是否有该key #python2:info.has_key("1103") 效果一样,但是3消失了

 

#判断key会否存在,存在就不进行替换,不存在就添加

info. setdefault('stu1103', "XiaoZe Maliya")

 

#字典合并

info = {

    'stu1101': "TengLan Wu",

    'stu1102': "LongZe Luola",

    'stu1103': "XiaoZe Maliya",

}

 

b ={

    'stu1101':"Burgess",

    1:3,

    2:5

}

 

info.update(b)  #把b该字典更新到info字典内(合并起来) 相同key 那么会吧原先的字典值覆

 

#使用.fromkeys创建新字典(一旦字典的value一修改,所有的都会修改)

c = dict.fromkeys([6,7,8],"test") 

#结果c=={8: 'test', 6: 'test', 7: 'test'}

d = dict.fromkeys([6,7,8],[1,{"name":"burgess"},444]) 

#结果d=={8: [1, {'name': 'burgess'}, 444], 6: [1, {'name': 'burgess'}, 444], 7: [1, {'name': 'burgess'}, 444]}

d[8][1]['name'] = "Jack "

#结果d=={8: [1, {'name': 'Jack '}, 444], 6: [1, {'name': 'Jack'}, 444], 7: [1, {'name': 'Jack'}, 444]}

 

 

#Python字典列表取值

__author__ = "Burgess Zheng"

USER_DICT = {

    '1':{'name':'root1','email':'[email protected]'},

    '2':{'name':'root2','email':'[email protected]'},

    '3':{'name':'root3','email':'[email protected]'},

    '4':{'name':'root4','email':'[email protected]'},

    '5':{'name':'root5','email':'[email protected]'},

}

for i in USER_DICT.keys():  #循环取该字典的key

    print(i)  #打印结果是 1 2.....

for i in USER_DICT.values():  #循环取该字典的value

    print(i)  #打印结果是([{'name':'root1','email':'[email protected]'},{‘name’:root2....}]

for k,i in USER_DICT.items():#获取key value

    print(i)  #打印value  结果{'email': '[email protected]', 'name': 'root2'}

    print(k)  #打印key   结果2

 

print(USER_DICT["1"])#打印key=1value

                    # 打印结果 {'name': 'root1', 'email': '[email protected]'}

print(USER_DICT["1"]["email"])#打印key=1valuekey=emailvalue

                    # 打印结果 [email protected]

print(list(USER_DICT.values()))

                   # 打印结果 [{'name': 'root1', 'email': '[email protected]'},{‘name’:root2....},....]

print(list(USER_DICT.key()))

                   # 打印结果 [‘1’,’2’....’5’]

print(list(a.items()))

                   # 打印结果 [(‘1’,{‘name’:root1....),(‘2’,{‘name’:root2...}),()...]

 

USER_LIST = [

    {'name':'root1','email':'[email protected]'},

    {'name':'root2','email':'[email protected]'},

    {'name':'root3','email':'[email protected]'},

    {'name':'root4','email':'[email protected]'},

    {'name':'root5','email':'[email protected]'},

]

for i in USER_LIST:#循环取该列表所有的元素

    print(i)#打印结果是{'name': 'root1', 'email': '[email protected]'}

 

print(USER_LIST[0])  #打印该列表下标0value

                    #结果:{'email': '[email protected]', 'name': 'root1'}

print(USER_LIST[0]["name"])#打印该列表下标0valuekey=name的值

                                                        #结果:root1

 

#sorted 升序

a = {6:2,8:0,1:4,-5:6,99:11,4:22}

print(a)  #我们知道字典是无序的

print(sorted(a))   #默认key升序,变成列表 value消失

print(sorted(a.items()))  #默认key升序,变成列表 value存在

print(sorted(a.items(),key=lambda x:x[1]))  #按value升序 变成列表

#key=lambda x=key:x[1]取该key的value  等于字典的key作为传参

#要排序就必须变成列表 items就是变成列表

 

猜你喜欢

转载自blog.csdn.net/Burgess_zheng/article/details/85688465