字典,{key,value}

help(dict)

  • 定义一个字典:
>>> dict1
{'salary': '', 'age': '', 'name': 'yzw'}
>>> dict2=dict1
>>> id(dict2)
139948810542280
>>> id(dict1)
139948810542280
>>> dict2=dict1.copy()
>>> id(dict1),id(dict2)
(139948810542280, 139948887698696)
  • |  clear(...)  清空一个字典
     |      D.clear() -> None.  Remove all items from D.

    >>> dict2.clear()
    >>> dict2
    {}
  • |  copy(...)  普通赋值指向相同的内存地址
     |      D.copy() -> a shallow copy of D
    >>> dict2=dict1.copy()
    >>> id(dict1),id(dict2)
    (139948810542280, 139948887698696)
  • |  fromkeys(iterable, value=None, /) from builtins.type  创建一个以序列为key,值相同的字典
     |      Returns a new dict with keys from iterable and values equal to value.

    >>> seq1=('length','width','square')
    >>> dict3=dict.fromkeys(seq1)
    >>> dict3
    {'length': None, 'width': None, 'square': None}
    >>> dict4=dict.fromkeys(seq1,10)
    >>> dict4
    {'length': 10, 'width': 10, 'square': 10}
  • |  get(...)  根据key取value,key不存在则返回None   dict[key]取值key不存在时报错
     |      D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
    >>> dict1
    {'salary': '', 'age': '', 'name': 'yzw'}
    >>> dict1.get('salary')
    ''
    >>> dict1.get('sex')
    >>> dict1['salary']
    '100'
    >>> dict1['sex']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'sex'
  • |  items(...)  以列表返回可遍历的(键, 值) 元组数组
     |      D.items() -> a set-like object providing a view on D's items
    >>> dict1.items()
    dict_items([('salary', ''), ('age', ''), ('name', 'yzw')]) 
  • |  keys(...)  返回所有key的列表
     |      D.keys() -> a set-like object providing a view on D's keys
    >>> dict1.keys()
    dict_keys(['salary', 'age', 'name'])   
  • |  pop(...)  删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值
     |      D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
     |      If key is not found, d is returned if given, otherwise KeyError is raised
    >>> dict1.pop('salary')
    ''
    >>> dict1
    {'age': '', 'name': 'yzw'}
    >>> dict1.pop('sex')
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    KeyError: 'sex'
    >>> dict1['sex']='man'
    >>> dict1
    {'sex': 'man', 'age': '18', 'name': 'yzw'}
  • |  popitem(...) 随机删除一个k/v
     |      D.popitem() -> (k, v), remove and return some (key, value) pair as a
     |      2-tuple; but raise KeyError if D is empty.
    >>> dict1
    {'square': None, 'kind': 'human', 'width': None, 'name': 'yzw', 'length': None, 'sex': 'man', 'age': ''}
    >>> dict1.popitem()
    ('square', None)
    >>> dict1.popitem()
    ('kind', 'human')
    >>> dict1
    {'width': None, 'name': 'yzw', 'length': None, 'sex': 'man', 'age': ''}
  • |  setdefault(...)  不存在key,则增加到字典中,若存在,则返回key对应的value
     |      D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
    >>> dict1.setdefault('kind','human')
    'human'
    >>> dict1
    {'kind': 'human', 'sex': 'man', 'age': '', 'name': 'yzw'}
    >>> dict1.setdefault('sex','femeal')
    'man'
    >>> dict1
    {'kind': 'human', 'sex': 'man', 'age': '18', 'name': 'yzw'}
  • |  update(...)  字典合并,不能存在的k/v则增加,已存在的k则覆盖更新
     |      D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
     |      If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]
     |      If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v
     |      In either case, this is followed by: for k in F:  D[k] = F[k]
    >>> dict1
    {'kind': 'human', 'sex': 'man', 'age': '', 'name': 'yzw'}
    >>> dict3
    {'length': None, 'width': None, 'square': None}
    >>> dict3['kind']='human'
    >>> dict3
    {'kind': 'human', 'length': None, 'width': None, 'square': None}
    >>> dict1.update(dict3)
    >>> dict1
    {'square': None, 'kind': 'human', 'width': None, 'name': 'yzw', 'length': None, 'sex': 'man', 'age': ''}
    >>>
  • |  values(...)  返回字典所有值的列表形式
     |      D.values() -> an object providing a view on D's values
    >>> dict1.values()
    dict_values([None, 'human', None, 'yzw', None, 'man', ''])
  • 字典遍历

    >>> dict1
    {'width': None, 'name': 'yzw', 'length': None, 'sex': 'man', 'age': ''}
    >>> for i in dict1:
    ... print(i,dict1[i])
    ...
    width None
    name yzw
    length None
    sex man
    age 18
    # 效率最高
    >>> for k,v in dict1.items():
    ... print(k,v)
    ...
    width None
    name yzw
    length None
    sex man
    age 18
    #效率低
    >>> for k,v in enumerate(dict1):
    ... print(k,v)
    ...
    0 width
    1 name
    2 length
    3 sex
    4 age
    >>> for idx,k in enumerate(dict1):
    ...     print(idx,k,dict1[k])
    ...
    0 width None
    1 name yzw
    2 length None
    3 sex man
    4 age 18

python3.x 基础一:dict字典的更多相关文章

  1. python基础(六)dict字典和文件操作open

    字典dict 使用key来标注value的数据类型,key和value是一一对应的.在字典中key是唯一的,所以字典也是无序的. #定义一个字典 dict = { 'name' : 'sylar', ...

  2. python基础数据类型--dict 字典

    字典 字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必 ...

  3. Python - 基础数据类型 dict 字典

    字典简介 字典在 Python 里面是非常重要的数据类型,而且很常用 字典是以关键字(键)为索引,关键字(键)可以是任意不可变类型 字典由键和对应值成对组成,字典中所有的键值对放在 { } 中间,每一 ...

  4. Python基础数据类型(五) dict字典

    字典dict{} 字典数字自动排序 enumerate 枚举 for i,k in enumerate(dic,1) #第二个参数默认不写就是0 ---枚举 print(i,k) dict,以{}来表 ...

  5. Python3中dict字典的相关操作函数

    字典对象的内建函数 1. clear() 清空字典. 例: >>> a = {1:3, 2:4} >>> a.clear() >>> a {} 2 ...

  6. python的dict()字典数据类型的方法详解以及案例使用

    一.之前的回顾 # int  数字 # str 字符串 # list 列表 # tuple 元组 # dict 字典 字典中最重要的方法 keys() values() items() get upd ...

  7. Python基础知识(五)------字典

    Python基础知识(四)------字典 字典 一丶什么是字典 ​ dict关键字 , 以 {} 表示, 以key:value形式保存数据 ,每个逗号分隔 ​ 键: 必须是可哈希,(不可变的数据类型 ...

  8. python基础之dict、set及字符

    python基础之dict.set及字符串处理 本节内容 字典介绍及内置方法 集合介绍 字符串处理 1.字典介绍及内置方法 字典是python中唯一的映射类型,采用键值对(key-value)的形式存 ...

  9. python基础——使用dict和set

    python基础——使用dict和set dict Python内置了字典:dict的支持,dict全称dictionary,在其它语言中也称为map(映射),使用键-值(key-value)存储,具 ...

  10. 第四篇:python基础之dict、set及字符

    python基础之dict.set及字符   python基础之dict.set及字符串处理 本节内容 字典介绍及内置方法 集合介绍 字符串处理 1.字典介绍及内置方法 字典是python中唯一的映射 ...

随机推荐

  1. vs code 打开文件时,取消文件目录的自动定位跟踪

    文件-->首选项-->设置-->在搜索栏中搜索:explorer.autoReveal;    去掉勾选即可.

  2. thymeleaf 模板语法

    模板语法 如何在 script 标签体内部使用 th 获取后端数据 添加如下属性 <script type="text/javascript" th:inline=" ...

  3. 理解分布式一致性:Paxos协议之Multi-Paxos

    理解分布式一致性:Paxos协议之Multi-Paxos Multi-Paxos without failures Multi-Paxos when phase 1 can be skipped Mu ...

  4. 【linux三剑客】sed命令

    sed - stream editor for filtering and transforming text sed 流编辑器 strem edition,实现对文件的增删改替换查是Linux中第二 ...

  5. USACO 2.1 海明码 Hamming Codes (模拟+位运算+黑科技__builtin_popcount(n))

    题目描述 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个单位 ...

  6. Top 命令数据分析

    一.top 命令详解 当前时间 20:27:12 当前系统运行时间 3:18秒 1个用户 系统负载平均长度为 0.00,0.00,0.00(分别为1分钟.5分钟.15分钟前到现在的平均值) 第二行为进 ...

  7. 3) drf 框架生命周期 请求模块 渲染模块 解析模块 自定义异常模块 响应模块(以及二次封装)

    一.DRF框架 1.安装 pip3 install djangorestframework 2.drf框架规矩的封装风格 按功能封装,drf下按不同功能不同文件,使用不同功能导入不同文件 from r ...

  8. Azure AD(一)入门认识

    一,引言(吹水) 距离上一次介绍Azure Functions的相关博文以及过期快一个月了,本来早早都想好已经规划好的Azure的相关的学习的路线,无奈还是由于自己文笔不好以及自身太懒,导致博文没有更 ...

  9. tp5中使用ueditor编辑器保存文本到数据库后回显后显示html标签问题解决办法

    在编辑器ueditor中获取文本,保存到到数据库后为 当在数据库中提取出来,在显示回ueditor编辑器时候,出了问题, html标签都显示出来了 百度了下别人的解决办法是,使用官方提供的api 可是 ...

  10. Spring官网阅读 | 总结篇

    接近用了4个多月的时间,完成了整个<Spring官网阅读>系列的文章,本文主要对本系列所有的文章做一个总结,同时也将所有的目录汇总成一篇文章方便各位读者来阅读. 下面这张图是我整个的写作大 ...