zoukankan      html  css  js  c++  java
  • python字典和集合

    字典

    特点

    • 字典是无序的
    • 字典的key是唯一的

    字典的格式

    info = {
        'stu1101' : "TengLan Wu",
        'stu1102' : "LongZe LuoLa",
        'stu1103' : "XiaoZe Maliya",
    }

    当然也可以多层嵌套:

    av_catalog = {
        "欧美":{
            "www.youporn.com": ["很多免费的,世界最大的","质量一般"],
            "www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"],
            "letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"],
            "x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"]
        },
        "日韩":{
            "tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"]
        },
        "大陆":{
            "1024":["全部免费,真好,好人一生平安","服务器在国外,慢"]
        }
    }
    
    av_catalog["大陆"]["1024"][1] += ",可以用爬虫爬下来"
    print(av_catalog["大陆"]["1024"])
    #ouput 
    ['全部免费,真好,好人一生平安', '服务器在国外,慢,可以用爬虫爬下来']

    字典之增加

    >>> info["stu1104"] = "苍井空"
    >>> info
    {'stu1102': 'LongZe Luola', 'stu1104': '苍井空', 'stu1103': 'XiaoZe Maliya', 'stu1101': 'TengLan Wu'}

    字典之修改

    前提是在原有的key值上修改:

    >>> info['stu1101'] = "武藤兰"
    >>> info
    {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1101': '武藤兰'}

    字典之删除

    pop

    #删除的同时将值取回
    >>> info
    {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1101': '武藤兰'}
    >>> ret=info.pop("stu1101") #标准删除姿势
    ret='武藤兰'

    del

    #根据key值直接删除
    >>> info
    {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
    >>> del info['stu1103'] #换个姿势删除

    popitem

    >>> info
    {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} #随机删除
    >>>ret= info.popitem()
    print(ret)
    ###('stu1102', 'LongZe Luola')

    字典之查找

    >>> info = {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
    >>> 
    >>> "stu1102" in info #标准用法
    True
    >>> info.get("stu1102",'')  #获取,如果没有默认为''
    'LongZe Luola'
    >>> info["stu1102"] #同上,但是看下面
    'LongZe Luola'
    >>> info["stu1105"]  #如果一个key不存在,就报错,get不会,不存在只返回None
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'stu1105'

    其他操作

    keys() 获取字典所有key值

    #keys
    >>>dict_keys= info.keys()
    dict_keys--->(['stu1102', 'stu1103'])
    for key in dict_keys:
        print(key)

    values()获取字典所有的值

    #values
    >>> info.values()
    dict_values(['LongZe Luola', 'XiaoZe Maliya'])

    items() 将key和values放在元组中

    #items
    info.items()
    dict_items([('stu1102', '龙泽萝拉'), (1, 2), (3, 4), ('stu1103', 'XiaoZe Maliya'), ('stu1106', 'Alex')])
    
    for key,values in info.items():
        print(key,values)

    update 将两个字典合并

    • A字典updateB字典,A.update(B) 如果key重复,那么值以B字典的为准
    #update 
    >>> info
    {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
    >>> b = {1:2,3:4, "stu1102":"龙泽萝拉"}
    >>> info.update(b)
    >>> info
    {'stu1102': '龙泽萝拉', 1: 2, 3: 4, 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}

    setdefault 设置值,如果有对应的key值则设置失败

    >>> info.setdefault("stu1106","Alex")
    'Alex'
    >>> info
    {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
    >>> info.setdefault("stu1102","龙泽萝拉")
    'LongZe Luola'
    >>> info
    {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}

    集合

    集合是一个无序的,不重复的数据组合,它的主要作用如下:

    • 去重,把一个列表变成集合,就自动去重了
    • 关系测试,测试两组数据之前的交集、差集、并集等关系

    创建一个结合

    s=set([1,2,3,4,5,6,7])
    t=set([5,7,8,9,10])

    集合增删改查

    基本操作:  
    t.add('x')            # 添加一项  
      
    s.update([10,37,42])  # 在s中添加多项  
      
    使用remove()可以删除一项:  
    t.remove('H') 
    t.discard(1)    #删除元素,不存在该元素不会报错,如果是remove则报错
    len(s) set 的长度 x in s 测试 x 是否是 s 的成员 x not in s 测试 x 是否不是 s 的成员 

    集合的特性

    交集

    s=set([1,2,3,4,5,6,7])
    t=set([5,7,8,9,10])
    ret= s&t
    #ret=s.intersection(t)
    print(ret)
    #{5, 7}

    并集

    s=set([1,2,3,4,5,6,7])
    t=set([5,7,8,9,10])
    #并集
    print(s.union(t))
    print(s | t)
    #{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    差集

    s=set([1,2,3,4,5,6,7])
    t=set([5,7,8,9,10])
    print(s.difference(t))  #s里面有的t里面没有的元素
    print(t.difference(s))
    print(s - t)
    # {1, 2, 3, 4, 6}
    # {8, 9, 10}
    # {1, 2, 3, 4, 6}

    对称差集

    s=set([1,2,3,4,5,6,7])
    t=set([5,7,8,9,10])
    #对称差集
    print(s.symmetric_difference(t))  #两个并集减去交集
    print(s ^ t)
    #{1, 2, 3, 4, 6, 8, 9, 10}

    判断父子集

    s=set([1,2,3,4,5,6,7])
    t=set([1,2,3])
    #子集
    print(t.issubset(s)) #t是s的子集,返回true
    
    print(s.issuperset(t)) #s是t的父集,返回true
  • 相关阅读:
    bits,Bytes,KB,MB,GB和TB之间的换算关系
    idea快捷键
    拦截器Interceptor和过滤器Filter的区别
    JSTL标签
    EL 表达式
    El 表达式和 Jstl 标签库
    JavaWeb servlet,乱码的原因和解决
    java类从加载、连接到初始化过程
    js中获取监听键盘事件
    ASP.NET Core Web 支付功能接入 微信-扫码支付篇(转)
  • 原文地址:https://www.cnblogs.com/chenxuming/p/9474939.html
Copyright © 2011-2022 走看看