字典:反映对应关系的映射类型

  • 字典(dict)是包含若干“键:值”元素的无序可变序列

  • 字典中元素的“键”可以是python中任意不可变数据,例如整数、实数、复数、字符串、元组等类型可哈希数据,“键”不允许重复,“值”是可以重复的。字典在内部维护的哈希表使得检索操作非常快。

字典创建与删除
  • 使用“=”

    >>> aDict = {'server':'db.diveintopython3.org','database':'mysql'}
    >>> x = dict() # 创建空字典
    >>> x
    {}
    >>> y = {} # 创建空字典
    >>> keys = ['a','b','c','d']
    >>> values = [1,2,3,4]
    >>> dictionary = dict(zip(keys,values)) # 根据已有数据创建字典
    >>> dictionary
    {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    >>> d = dict(name = 'Dong', age = 39) # 以关键参数的形式创建字典
    >>> d
    {'name': 'Dong', 'age': 39}
    >>> aDict = dict.fromkeys(['name','age','sex']) # 以给定内容为“键”,创建值为空的字典
    >>> aDict
    {'name': None, 'age': None, 'sex': None}
字典推导式
  • 使用字典推导式快速生成符合特定条件的字典

    >>> {i:str(i) for i in range(1,5)}
    {1: '1', 2: '2', 3: '3', 4: '4'}
    >>> x = ['A','B','C','D']
    >>> y = ['a','b','c','d']
    >>> {i:j for i,j in zip(x,y)}
    {'A': 'a', 'B': 'b', 'C': 'c', 'D': 'd'}
字典元素的访问
  • 字典中每个元素表示一种映射关系或对应关系

    >>> aDict = {'age':30,'score':[98,97],'name':'Dong','sex':'male'}
    >>> aDic['age']
    >>> aDict['age']
    30
    >>> aDict['address'] # 字典中不存在该key时,抛出异常
    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
    KeyError: 'address'
    # 处理异常方式一:
    >>> if 'address' in aDict:
    ...   print(aDict['address'])
    ... else:
    ...   print('No Exists')
    ...
    No Exists
    # 处理异常方式二:
    >>> try:
    ...   print(aDict['address'])
    ... except:
    ...   print('No Exist')
    ...
    No Exist
  • get()方法:返回指定“键”的值,并且允许指定该键不存在时返回特定的“值”

    >>> aDict.get('age')
    30
    >>> aDict.get('adress','No Exist.') # 指定键不存在时返回指定默认值
    'No Exist.'
    >>> import string
    >>> import random
    >>> x = string.ascii_letters + string.digits
    >>> z = ''.join((random.choice(x) for i in range(1000)))
    >>> d = dict()
    >>> for ch in z: # 遍历字符串统计词频
    ...   d[ch] = d.get(ch,0) + 1
    ...
    >>> for k,v in sorted(d.items()): # 查看统计结果
    ...   print(k,':',v)
    ...
    0 : 13
    1 : 19
    2 : 17
    3 : 17
    4 : 19
    5 : 25
    6 : 21
    7 : 12
    8 : 17
    9 : 17
    A : 11
    B : 20
    C : 15
    D : 21
    E : 22
    F : 9
    G : 15
    H : 12
    I : 9
    J : 16
    K : 13
    L : 16
    M : 19
    N : 14
    O : 17
    P : 11
    Q : 14
    R : 16
    S : 11
    T : 22
    U : 13
    V : 20
    W : 21
    X : 17
    Y : 14
    Z : 21
    a : 17b : 9c : 17d : 15e : 14f : 11g : 18h : 20i : 21j : 19k : 20l : 9m : 16n : 10o : 13p : 14q : 25r : 17s : 12t : 20u : 10v : 20w : 17x : 10y : 15z : 25
  • setdefault()方法:用于返回指定“键”对应的值,如果字典中不存在该“键”,就添加一个新元素并设置该“键”对应的“值”(默认为None)

    >>> aDict.setdefault('adress','SDIBT')
    'SDIBT'
    >>> aDict
    {'age': 30, 'score': [98, 97], 'name': 'Dong', 'sex': 'male', 'adress': 'SDIBT'}
    >>> aDict.setdefault('age',23)
    30
  • 对字典直接进行迭代或者遍历时默认是遍历字典的“键”

    >>> aDict
    {'age': 30, 'score': [98, 97], 'name': 'Dong', 'sex': 'male', 'adress': 'SDIBT'}
    >>> for item in aDict: # 默认遍历字典的“键”
    ...   print(item,end=' ')
    ...
    age score name sex adress >>>
    >>> for item in aDict.items(): # 明确指定遍历字典的元素
    ...   print(item,end=' ')
    ...
    ('age', 30) ('score', [98, 97]) ('name', 'Dong') ('sex', 'male') ('adress', 'SDIBT') >>>
    >>> aDict.items()
    dict_items([('age', 30), ('score', [98, 97]), ('name', 'Dong'), ('sex', 'male'), ('adress', 'SDIBT')])
    >>> aDict.keys()
    dict_keys(['age', 'score', 'name', 'sex', 'adress'])
    >>> aDict.values()
    dict_values([30, [98, 97], 'Dong', 'male', 'SDIBT'])
元素的添加、修改与删除
  • 当以指定“键”为下标为字典元素赋值时,该键存在表示修改,不存在表示添加

    >>> aDict = {'age': 30, 'score': [98, 97], 'name': 'Dong', 'sex': 'male', 'adress': 'SDIBT'}
    >>> aDict['age'] = 39
    >>> aDict
    {'age': 39, 'score': [98, 97], 'name': 'Dong', 'sex': 'male', 'adress': 'SDIBT'}
    >>> aDict['school'] = 'sichuandaxue'
    >>> aDict
    {'age': 39, 'score': [98, 97], 'name': 'Dong', 'sex': 'male', 'adress': 'SDIBT', 'school': 'sichuandaxue'}
  • update()方法可以将另外一个字典的“键:值”一次性全部添加到当前字典对象,如果两个字典中存在相同的“键”,则以另一个字典中的“值”为准对当前字典进行更新

    >>> aDict = {'age': 30, 'score': [98, 97], 'name': 'Dong', 'sex': 'male', 'adress': 'SDIBT'}
    >>> aDict.update({'a': 87, 'age':39})
    >>> aDict
    {'age': 39, 'score': [98, 97], 'name': 'Dong', 'sex': 'male', 'adress': 'SDIBT', 'a': 87}
  • del命令删除字典中指定的元素

    >>> aDict = {'age': 30, 'score': [98, 97], 'name': 'Dong', 'sex': 'male', 'adress': 'SDIBT'}
    >>> del aDict['adress']
    >>> aDict
    {'age': 30, 'score': [98, 97], 'name': 'Dong', 'sex': 'male'}
    >>> del aDict
    >>> aDict
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'aDict' is not defined
  • 字典对象的pop()和popitem()方法可以弹出并删除指定的元素

    >>> aDict = {'age': 30, 'score': [98, 97], 'name': 'Dong', 'sex': 'male', 'adress': 'SDIBT'}
    >>> aDict.pop('name') # 弹出指定键对应的值
    'Dong'
    >>> aDict
    {'age': 30, 'score': [98, 97], 'sex': 'male', 'adress': 'SDIBT'}
    >>> aDict.popitem() # 弹出一个元素
    ('adress', 'SDIBT')
    >>> aDict
    {'age': 30, 'score': [98, 97], 'sex': 'male'}
标准库collections中与字典有关的类
  • OrderedDict类

    字典dict是无序的,如果需要一个可以记住元素插入顺序的字典,可以使用collections.OrderedDict

    >>> import collections
    >>> x = collections.OrderedDict()
    >>> x['a'] = 3
    >>> x['b'] = 5
    >>> x['c'] = 8
    >>> x
    OrderedDict([('a', 3), ('b', 5), ('c', 8)])
  • defaultdict类

    字母出现频次统计问题,也可以使用collections模块的defaultdict类来实现

    >>> import string
    >>> import random
    >>> x = string.ascii_letters+string.digits+string.punctuation
    >>> z = ''.join([random.choice(x) for i in range(100)])
    >>> from collections import defaultdict
    >>> frequences = defaultdict(int)     # 所有值默认为0
    >>> frequences
    defaultdict(<class 'int'>, {})
    >>> for item in z:
    ...   frequences[item] += 1
    ...
    >>> frequences.items()
    dict_items([('F', 1), ('[', 2), ('q', 1), ('>', 2), ('d', 5), ('`', 1), ('e', 2), ('!', 3), ('A', 1), ('R', 1), ('Z', 2), ('V', 2), ('g', 2), ('n', 2), ('2', 1), ('w', 1), ('|', 1), ('v', 3), ('c', 2), ('u', 3), ('&', 4), ('m', 2), ('S', 2), (',', 2), ('@', 3), ('$', 2), ('{', 1), ('j', 1), ('\\', 1), ('~', 1), ('U', 1), ('=', 1), ('M', 4), ('l', 1), ('^', 1), ('}', 1), (']', 2), ('0', 1), ('+', 2), ('(', 1), ('"', 1), ('Q', 1), ('4', 2), ('.', 1), ('x', 1), ("'", 1), ('<', 2), ('/', 2), (';', 1), ('E', 1), (')', 1), ('o', 1), ('P', 1), ('W', 1), ('B', 1), ('K', 1), ('8', 1), ('_', 1), ('N', 1), ('h', 1), ('7', 1), ('I', 1), ('G', 1), ('*', 1), ('y', 1)])

    创建defaultdict对象时,传递的参数表示字典中值的类型

    >>> from collections import defaultdict
    >>> games = defaultdict(list)
    >>> games
    defaultdict(<class 'list'>, {})
    >>> games['name'].append('dong')
    >>> games['name'].append('zhang')
    >>> games['score'].append(90)
    >>> games['score'].append(93)
    >>> games
    defaultdict(<class 'list'>, {'name': ['dong', 'zhang'], 'score': [90, 93]})
  • Counter类

    对于词频统计的问题,使用collections模块的Counter类可以更加快速地实现这个功能,并且能够提供更多的功能,例如,查找出现次数最多的元素

    >>> import string
    >>> import random
    >>> x = string.ascii_letters+string.digits+string.punctuation
    >>> z = ''.join([random.choice(x) for i in range(100)])
    >>> from collections import Counter
    >>> frequences = Counter(z)
    >>> frequences.items()
    dict_items([('H', 12), ('%', 18), ('K', 13), ('A', 12), ('\\', 6), ('N', 11), ('2', 14), ('y', 13), ('z', 12), ('T', 10), (':', 8), ('m', 8), ("'", 11), ('R', 12), (',', 10), ('E', 7), ('e', 16), ('b', 10), ('f', 16), ('+', 8), ('7', 15), ('v', 9), ('l', 15), ('"', 9), ('.', 12), ('^', 20), ('_', 16), ('>', 7), ('h', 12), ('C', 12), ('p', 13), ('n', 8), ('Y', 14), ('L', 11), ('O', 12), ('{', 5), ('3', 10), (')', 15), ('}', 4), ('|', 14), ('a', 10), ('@', 9), ('w', 10), ('B', 11), ('6', 8), ('Q', 11), ('`', 10), ('/', 8), ('<', 5), ('=', 12), ('M', 12), ('4', 6), ('s', 18), ('[', 7), ('G', 12), ('#', 16), ('o', 13), ('*', 8), ('i', 16), ('P', 12), ('k', 17), ('j', 4), ('-', 15), ('D', 4), (']', 6), ('q', 16), ('$', 17), ('J', 15), ('U', 14), ('t', 11), ('I', 11), ('0', 7), ('r', 12), ('&', 6), ('!', 12), ('u', 10), ('F', 9), ('W', 6), ('c', 11), ('1', 8), ('5', 6), (';', 5), ('V', 12), ('~', 10), ('Z', 11), ('d', 9), ('9', 9), ('X', 13), ('8', 9), ('?', 5), ('S', 6), ('x', 7), ('(', 7), ('g', 6)])>>> frequences.most_common(1) # 返回出现次数最多的一个字符及其词频[('^', 20)]>>> frequences.most_common(3)[('^', 20), ('%', 18), ('s', 18)]>>> frequences.most_common(10)[('^', 20), ('%', 18), ('s', 18), ('k', 17), ('$', 17), ('e', 16), ('f', 16), ('_', 16), ('#', 16), ('i', 16)]>>> z = ''.join([random.choice(x) for i in range(10000)])>>> frequences = Counter(z)>>> frequences.most_common(10)[('O', 127), ('c', 125), ('5', 121), ('-', 121), ('\\', 121), ("'", 120), ('~', 118), (',', 118), ('J', 118), ('<', 117)]​>>> z = [1,2,3,4,1,'a','v','wer','wer','wer',1]>>> frequences = Counter(z)>>> frequences.most_common(3)[(1, 3), ('wer', 3), (2, 1)]

Python序列结构--字典的更多相关文章

  1. Python序列结构

    python中常用的序列结构由列表.元组.字典.字符串.集合等,列表.元组.字符串等有序序列以及range对象均支持双向索引 是否有序 序列结构 是否是可变序列 有序序列 元组 不可变序列 有序序列 ...

  2. python 序列结构-列表,元组,字典,字符串,集合

    列表 """ name_list.__add__( name_list.__getslice__( name_list.__new__( name_list.append ...

  3. Python序列结构--集合

    集合:元素之间不允许重复 集合属于Python无序可变序列,元素之间不允许重复 集合对象的创建与删除 直接将值赋值给变量即可创建一个集合 >>> a = {3,5}>>& ...

  4. python序列,字典备忘

    初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...

  5. Python序列结构--列表(一)

    列表 列表**包含若干元素的有序连续内存空间**,当列表增加或删除元素时,**列表对象自动进行内存的扩展或收缩**,从而**保证相邻元素之间没有缝隙**.但插入和删除非尾部元素时涉及列表元素大量的移动 ...

  6. Python序列结构--元组

    元组:轻量级列表 元组创建于元素访问 >>> x = (1, 2, 3)>>> type(x)<class 'tuple'>>>> x ...

  7. python 序列与字典

    序列概念: 序列的成员有序排列,可以通过下标访问到一个或几个元素,就类似与c语言的数组. 序列的通用的操作: 1:索引 11 = [1,2,3,4] 11[0] = 1 2:切片 11[1,2,3,4 ...

  8. Python列表,元组,字典,序列,引用

    1.列表 # Filename: using_list.py # This is my shopping list shoplist=["apple", "mango&q ...

  9. Python的dict字典结构操作方法学习笔记

    Python的dict字典结构操作方法学习笔记 这篇文章主要介绍了Python的dict字典结构操作方法学习笔记本,字典的操作是Python入门学习中的基础知识,需要的朋友可以参考下 一.字典的基本方 ...

随机推荐

  1. Auzone AT60 TPMS Tool Update & Authorization Service: FREE

    This is a tutorial with step-of-step explanation of Auzone AT60 TPMS Tool Update & Authorization ...

  2. Node.js 薄荷网爬取

    Node.js:是一个基于前端的服务器,主要的特点:单线程,异步I/O(对这个没有了解,开发起来真的会踩很多坑),事件驱动 前言:本人主要是一个以使用.Net平台下的语言,进行开发的一个菜鸡,之前面试 ...

  3. flock - 必应词典

    flock - 必应词典 美[flɑk]英[flɒk] v.聚集:群集:蜂拥 n.(羊或鸟)群:(尤指同类人的)一大群 网络羊群:大量:羊群,一群 变形复数:flocks:过去分词:flocked:现 ...

  4. H-Modify Minieye杯第十五届华中科技大学程序设计邀请赛现场赛

    题面见 https://ac.nowcoder.com/acm/contest/700#question 题目大意是有n个单词,有k条替换规则(单向替换),每个单词会有一个元音度(单词里元音的个数)和 ...

  5. 19-02【mac电脑操作】最小化应用程序

    最小化应用程序 windows下很简单,直接使用windows+M即可: mac电脑下,官方建议是:option+command+m+h.但实际使用的时候,这个快捷键并不好使: 解决方案:mac系统设 ...

  6. mysql设置存储中文变成问号或者乱码

    技术交流群: 816227112 问题: 解决办法: 修改my.ini  如果是my-default.ini 要重命名成my.ini 要注意顺序,有可能服务启动不起来 [mysqld] charact ...

  7. VNF网络性能提升解决方案及实践

    VNF网络性能提升解决方案及实践 2016年7月 作者:    王智民 贡献者:     创建时间:    2016-7-20 稳定程度:    初稿 修改历史 版本 日期 修订人 说明 1.0 20 ...

  8. V2019 Super DSP3 Odometer Correction Vehicle List

    Comparing v2017 Super DSP3 mileage programmer, the newest V2019 Super DSP III adds newer vehicles, i ...

  9. day 8:open文件和with的使用

    本节内容: 1,open打开文件后的几种操作 2,with和open的连用 3,flush的使用 1:open 1)r权限 f = open("D:\\auto\project\\fulls ...

  10. [Python] 建 Django 项目

    Python和Django的安装见这里:http://www.runoob.com/django/django-install.html 安装 Django 之后,您现在应该已经有了可用的管理工具 d ...