zoukankan      html  css  js  c++  java
  • python数据类型之三

    字典

    字典的基本结构

    #   字典, 键值对 dict类
    # 字典的基本结构
    # 字典的值可以是任何值
    # 字典的键不能是列表,字典, 最好也不要用布尔值(可能会和1和0重复)
    # 字典无序,
    my_dict = {
        "k1": "v1",  # 键值对
        "k2": "v2",
        "k3": 12,
        "k4": [
            1, 2,
            [],
            (),
            {
                "kk1": 11,
                "kk2": 12,
            }
        ],
        (1, 2, 3): "数字"
    }
    print(my_dict)


    索引找到指定元素 提取v1, 11

    v = my_dict["k1"]
    v2 = my_dict['k4'][4]["kk1"]
    print(v, v2)
    #   字典支持del删除
    del my_dict["k2"]


    遍历字典

    #   默认遍历key, 这里显示的是 所有的key
    #   等价于 in my_dict.keys(), 同样也有  in my_dict.values()
    for item in my_dict:
        print(item)
    
    for item in my_dict:
        print(item, my_dict[item])
    #   遍历字典二
    for key, value in my_dict.items():
        print(key, value)
    
    dic = {
        "k1": 'v1',
        "k2": 'v2',
    
    
    

    遍历字典的常用函数 kyes(), values(), items(), update(),get()

    # def update(self, E=None, **F)
    #   更新字典, 有的 改变, 没有的自动添加
    dic.update({'k2':1234, 'k10':10})
    print(dic)
    
    #   根据key获取值,当key不存在时可以指定默认值,默认值不存在时(None)
    # def get(self, k, d=None)
    # my_dict.get('k1')
    keys(), values(), items()参考上面的遍历字典

    字典的其他函数

    # def clear(self)
    # def copy(self)
    
    # @staticmethod  # known case  静态方法, 可以直接用dict调用
    # def fromkeys(*args, **kwargs)
    #   根据序列,创建字典 以你个的参数作为键值对
    v = dict.fromkeys([1,2,3], [4,5])
    print(v)
    
    
    # def setdefault(self, k, d=None)
    #   设置值, 如果key不存在, 就添加,('k5':12) 存在就不变
    dic.setdefault('k5', 12)
    print(dic)

    集合 set

    集合的基本结构

                集合:可以包含多个元素,用逗号分割,
                集合的元素遵循三个原则:
                 1:每个元素必须是不可变类型(可hash,可作为字典的key)
                 2:没有重复的元素
                 3:无序
    
    注意集合的目的是将不同的值存放到一起,不同的集合间用来做关系运算,无需纠结于集合中单个值
    #   不同元素组成, 无序, 集合中必须是不可变类型,(int, str, 元组)
    #优先掌握的操作:
    #1、长度len
    #2、成员运算in和not in
    
    #3、|合集
    #4、&交集
    #5、-差集
    #6、^对称差集
    #7、==
    #8、父集:>,>= 
    #9、子集:<,<=
    s = {1, 2, 3, 4, 5}
    print(s)
    # set
    s = set('123445')
    print(s)
    s = set(['haha', 'heihi', 'heng'])
    print(s)

    集合的基本操作

    #   添加一个元素
    # def add(self, *args, **kwargs)
    s.add(('123', 'heihi', 'ni'))
    print(s)
    
    #   清空
    # def clear(self, *args, **kwargs)
    #   浅拷贝
    # def copy(self, *args, **kwargs):
    
    # pop随机删除, remove删除指定的, remove如果删除的元素不存在 就回报错
    # def pop(self, *args, **kwargs)
    # s.pop()
    # s.remove('123')
    # def remove(self, *args, **kwargs)
    #   删除一个元素, 如果元素不存在 也不报错
    # def discard(self, *args, **kwargs)

    集合的并交差运算

    #   集合运算,并交差
    
    p = ['a', 'b', 'c', 'd']
    l = ['a', 'c', 'e']
    p_s = set(p)
    l_s = set(l)
    #   求交集s1&s2
    #   def intersection(self, *args, **kwargs)
    print(p_s.intersection(l_s))
    print(p_s & l_s)
    
    #   求差集  def difference(self, *args, **kwargs)
    #   p_s 中有而 l_s没有的
    print(p_s.difference(l_s))
    print(p_s - l_s)
    
    #   求并集
    print(p_s.union(l_s))
    print(p_s | l_s)
    
    #   求交叉补集
    print(p_s.symmetric_difference(l_s))
    print(p_s ^ l_s)
    
    #   补充
    # p_s.difference_update(l_s)
    # 等价于
    # p_s = p_s - l_s
    #   判断子集, 也有>= isup
    print(p_s.issubset(l_s))
    print(p_s <= l_s)

    利用集合去重

    #   简单去重操作
    my_list = [1, 2, 3, 'a', 'b',  1, 2, 'a']
    # m_s = set(my_list)
    my_list = list(set(my_list))

    基本数据类型常用方法总结

    # 常用数据类型的方法
    # int 常用 int()
    # 字符串
    # find/replace/join/strip/startwith/split/upper/lower/format(in)
    # 列表
    # append/extend/insert, 索引,切片,循环(in)
    # 元组
    # 忽略, 但是要知道 索引,切片,循环, 一级元素 不可以修改(in)
    # 字典
    # get/update/keys/values/items, 循环,索引, (in)
    # 判断是否存在,dic可以是字典,列表,字符串,元组
    # v = 'aa'in dic

    # 布尔值
    # bool()
    # 其中为空的有None, '', (),[],{},0 ==>False

  • 相关阅读:
    Zojax.com
    holtwick/pyxer
    深入学习爬虫的三个方向
    阅读gunicorn代码文档¶
    A database of opensource HTTP proxies written in python: htfilter2
    WebDebug: Enhancements
    北京网康科技有限公司关于网康
    用CONNECT方法突破HTTP代理服务器的扩展名封锁(python) [转] MyNewID 博客园
    NAT功能的用途是什么?有没有实例举一下?_百度知道
    wsgiref — WSGI Utilities and Reference Implementation¶
  • 原文地址:https://www.cnblogs.com/xiaokang01/p/9021755.html
Copyright © 2011-2022 走看看