• python(字典函数/操作)


    一、字典操作

    1.往字典中添加键值对

    • dict["key"] = "value"
    stu = {"name":"zhangsan","age":"18"}
    stu["sex"] = "boy"
    print (stu)
    • setdefault()
      • 有返回值
    stu = {"name":"zhangsan","age":"18"}
    stu.setdefault("sex")
    print (stu)    #结果 {'age': '18', 'name': 'zhangsan', 'sex': None}
    
    stu = {"name":"zhangsan","age":"18"}
    stu.setdefault("sex","boy")
    print (stu)  #结果 {'age': '18', 'name': 'zhangsan', 'sex': 'boy'}
    #添加的键存在时
    stu = {"name":"zhangsan","age":"18"}
    ret = stu.setdefault("age","20")
    print(ret)
    print(stu)
    
    ---> 18    #setdefault 返回值为改键已存在的值
    ---> {'age': '18', 'name': 'zhangsan'}    #键中的值不会被修改
    
    
    #添加的键不存在时
    stu = {"name":"zhangsan","age":"18"}
    ret = stu.setdefault("sex","boy")
    print(ret)
    print(stu)
    
    ---> boy    #setdefault 返回值为被添加键所对应的值
    ---> {'age': '18', 'sex': 'boy', 'name': 'zhangsan'}

    2.替换字典中指定键的值

    • dict["key"] = "value"
    stu = {"name":"zhangsan","age":18,"sex":"boy"}
    #替换值boy为gril
    stu["sex"] = "girl"
    print (stu)

    3.update()一个字典所有项追加到另一个字典里

    a = {"name":"zhangsan","sex":"boy"}
    b = {"age":18,"name":"lisi"}
    
    a.update(b)    #往字典a中追加字典b的内容
    print (a)   #当追加的键存在,则会覆盖之前的键所对应的值
    print (b)
    
    ---> {'sex': 'boy', 'age': 18, 'name': 'lisi'}
    ---> {'age': 18, 'name': 'lisi'}

    4.字典取值 

    • get()函数   -->   当提取的键不存在时,不会报错,为空
    a = {"name":"zhangsan","sex":"boy"}
    
    print (a.get("name"))
    #当取的键不存在时,不报错,返回值为None
    print (a.get("age"))
    
    #示例
    """
    dic = {"name1":"zhangsan","age":{"age1":"18","age2":20},"name2":"lisi","name3":"wangwu"}
    """
    • dict["key"]
    a = {"name":"zhangsan","sex":"boy"}
    
    print (a["name"])

    5.keys()从字典中取出所有键

    • 不能通过在括号中输入键来取键值
    • 返回所有的键,类型为 "dict_keys"
    stu = {"name":"zhangsan","age":"18"}
    key = stu.keys()
    print(key)          #打印出所有键序列
    print(type(key))    #返回类型为 dict_keys
    print(list(key))    #将 dict_keys 类型转换成列表
    
    ---> dict_keys(['age', 'name'])
    ---> <class 'dict_keys'>
    ---> ['age', 'name']

    6.values() 从字典中取出所有值

    • 返回所有的值,类型为 "dict_values"
    stu = {"name":"zhangsan","age":"18"}
    value = stu.values()
    print(value)          #打印出所有值序列
    print(type(value))    #返回类型为 dict_values
    print(list(value))    #将 dict_values 类型转换成列表
    
    ---> dict_values(['zhangsan', '18'])
    ---> <class 'dict_values'>
    ---> ['zhangsan', '18']

    7.items()以 dict_items 返回可遍历的(键, 值) 元组数组

    info = {"name":"zhangsan","sex":"boy"}
    
    print(info.items())
    print(type(info.items()))
    
    ---> dict_items([('sex', 'boy'), ('name', 'zhangsan')])
    ---> <class 'dict_items'>
    info = {"name":"zhangsan","sex":"boy"}
    
    for i in info.items():
        print(i)
    
    ---> ('name', 'zhangsan')
    ---> ('sex', 'boy')
    
    #-----------------------------------------
    
    info = {"name":"zhangsan","sex":"boy"}
    
    for x,y in info.items():
        print(x,y)
    
    ---> name zhangsan
    ---> sex boy

    8.del 删除键值对

    a = {"name":"zhangsan","sex":"boy","age":18}
    
    del a["name"]
    print (a)    #结果中"name":"zhansan"该键值对全部被删除

    9.pop()删除键值对

    • 括号中必须为键且不能为空
    • 返回值为删除键的值
    a = {"name":"zhangsan","sex":"boy","age":18}
    
    value = a.pop("name")
    print (value)    #返回值为"zhangsan"
    print (a)    #字典a中的"name":"zhangsan"键值对被删除

    10.popitem()随机删除字典第一个键值对

    • 并返回该键值对的元组
    a = {"name":"zhangsan","sex":"boy","age":18}
    
    b = a.popitem()
    print (b)        #返回值为包含被删除的键值对的一个元组
    print (a)        #字典a中的某个键值对被删除

    11.clear()清空字典中的键值对

    • 没有返回值
    • 原字典为空字典{}
    a = {"name":"zhangsan","sex":"boy","age":18}
    
    a.clear()
    print (a)

    12.copy()复制创建字典

    a = {"name":"zhangsan","sex":"boy","age":18}
    
    b = a.copy()
    print (b)        #b是a复制出来的字典

    13.has_key()检查字典是否有指定的键,有结果为 True,没有结果为 False

    • python3.0 中删除了此方法
    a = {"name":"zhangsan","sex":"boy","age":18}
    
    print a.has_key("age")    #True
    print a.has_key("height")   #False
    user = raw_input("账号:")
    pwd = raw_input("密码:")
    mysql.setdefault(user,pwd)
    print mysql
    if mysql.has_key(user):
        print "ok"
    else:
        print "no"
  • 相关阅读:
    Java中的 多线程编程
    Python中的字典详解
    Python中的数据类型
    Python中的字符串操作总结(Python3.6.1版本)
    R语言绘制沈阳地铁线路图
    HIVE中的order by操作
    Hive中order by,sort by,distribute by,cluster by的区别
    HDFS shell命令
    HDFS入门
    Bootstrap_Datatable Ajax请求两次问题的解决
  • 原文地址:https://www.cnblogs.com/ZhengYing0813/p/11727222.html
Copyright © 2020-2023  润新知