zoukankan      html  css  js  c++  java
  • 016 字典

    简介

    • 英文名:dict
    • 组合数据(“键值对”的形式),没有顺序

    创建

    例1 建空字典

    >>> d1 = {}
    >>> d2 = dict()
    >>> d1
    {}
    >>> d2
    {}
    >>> 
    

    例2 建有值的字典

    >>> d1 = {"one": 1, "two": 2, "three": 3}
    >>> d2 = dict({"one": 1, "two": 2, "three": 3})
    >>> d3 = dict(one=1, two=2, three=3)
    >>> d4 = dict([("one", 1), ("two", 2), ("three", 3)])
    >>> d1
    {'one': 1, 'two': 2, 'three': 3}
    >>> d2
    {'one': 1, 'two': 2, 'three': 3}
    >>> d3
    {'one': 1, 'two': 2, 'three': 3}
    >>> d4
    {'one': 1, 'two': 2, 'three': 3}
    >>> 
    
    • 还有一个 fromkeys() 方法,详见本文末尾

    特性

    • 字典是序列类型中的无序序列,所以不能分片和索引
    • 字典中的数据每个都由键值对组成,即 kv
      • key: 必须是可哈希的值,比如 int, str, float, tuple,但是 list, set, dict 不行
      • value: 任何值

    常见操作

    访问、更改与删除

    >>> d = {"one": 1, "two": 2, "three": 3}
    >>> d["one"]
    1
    >>> d["one"] = 100
    >>> d
    {'one': 100, 'two': 2, 'three': 3}
    >>> del d["one"]
    >>> d
    {'two': 2, 'three': 3}
    >>> 
    

    成员检测

    >>> d = {"one": 1, "two": 2, "three": 3}
    >>> "one" in d
    True
    >>> ("two", 2) in d
    False
    >>> 
    

    遍历

    >>> d = {"one": 1, "two": 2, "three": 3}
    >>> 
    >>> for k in d:
        print(k, d[k])
    
        
    one 1
    two 2
    three 3
    >>> 
    >>> for k in d.keys():
        print(k, d[k])
    
        
    one 1
    two 2
    three 3
    >>> 
    >>> for v in d.values():
        print(v)
    
        
    1
    2
    3
    >>> 
    >>> for k, v in d.items():
        print(k, v)
    
        
    one 1
    two 2
    three 3
    >>> 
    

    字典与生成式

    >>> d1 = {"one": 1, "two": 2, "three": 3}
    >>> d2 = {k: v for k, v in d1.items()}
    >>> d3 = {k: v for k, v in d1.items() if v % 2 == 1}
    >>> d2
    {'one': 1, 'two': 2, 'three': 3}
    >>> d3
    {'one': 1, 'three': 3}
    >>> 
    

    内置方法

    其它方法

    len(), max(), min()

    >>> d = {'a': 12, 'b': 36, 'c': 24}
    >>> len(d)
    3
    >>> max(d)  # 按 key 的字典序排列
    'c'
    >>> max(d, key=d.get)  # 按 value 的字典序排列
    'b'
    >>> 
    

    get(key, default=None)

    • 字典的内置方法
    • 释义:如果"键"在字典中,则返回键的值,否则,返回设定的 default
    >>> d = {"one": 1, "two": 2, "three": 3}
    >>> d.get("one")
    1
    >>> d.get("four")
    >>> d.get("four", "not found")
    'not found'
    >>> 
    >>> d.get("four", default="sorry")  # default 不是关键字参数
    Traceback (most recent call last):
      File "<pyshell#5>", line 1, in <module>
        d.get("four", default="sorry")
    TypeError: get() takes no keyword arguments
    >>> 
    

    fromkeys(iterable, value=None)

    • 释义:创建一个新字典,由 iterable 中的 kv 构成
    >>> a = ["one", "two", "three"]
    >>> d1 = dict.fromkeys(a)
    >>> d2 = dict.fromkeys(a, "num")
    >>> d1
    {'one': None, 'two': None, 'three': None}
    >>> d2
    {'one': 'num', 'two': 'num', 'three': 'num'}
    >>> 
    

    str(dict)

    >>> d = {"one":1, "two":2, "three":3}
    >>> str(d)
    "{'one': 1, 'two': 2, 'three': 3}"
    >>> 
    
  • 相关阅读:
    第十八周作业
    第十七周作业
    第十六周作业
    第十五周作业
    第十四周作业
    第十三周作业
    第十二周作业
    第二阶段考试
    第十周作业
    启航,带着梦想出发!
  • 原文地址:https://www.cnblogs.com/yorkyu/p/10293140.html
Copyright © 2011-2022 走看看