Python字典详解及功能介绍

文章目录

  • 1.函数
  • 2. 列表与元组
  • 3. 字典
  • 3.1 创建字典
  • 3.2 查找key
  • 3.3 新增修改元素
  • 3.4 删除元素
  • 3.5 遍历字典元素
  • 3.6 取出所有key和value
  • 3.7 合法的key类型
  • 1.函数

    函数

    2. 列表与元组

    列表与元组

    3. 字典

    字典是一种存储键值对的结构。
    和生活中的字典一样,当你查一个英语的意思时:apple就对应着苹果。它们就是一个键值对,其中apple就是key,而苹果就是value。
    这些键(key)和值(value)是一一对应的,我们可以根据键,快速找到值。

    3.1 创建字典

    创建一个空的字典,使用{}来表示字典。

    a = {}
    b = dict()
    print(type(a))
    print(type(b))
    
    '''
    <class 'dict'>
    <class 'dict'>
    '''
    
  • 可以在创建时同时指定初始值。
  • 键值对之间使用分割,键和值键使用:来分割。
  • 可以使用print来打印字典内容。
  • adict = {'apple': '苹果','world': '世界'}
    print(adict)
    
    # {'apple': '苹果', 'world': '世界'}
    

    如果你想要代码更加的美观,好看,可以这样写:

    adict = {
    	 'apple': '苹果',
    	 'world': '世界'
    }
    
    

    最后一个键值对,后面可写,也可不写。

    3.2 查找key

  • 使用in可以判断key是否在字典中存在,返回布尔值。
  • adict = {
    	 'apple': '苹果',
    	 'world': '世界'
    }
    print('apple' in adict)
    print('hello' in adict)
    '''
    True
    False
    '''
    
  • 使用[]通过类似下标访问的方式来获取元素的值,只不过这里的下标是key。
  • adict = {
    	 'apple': '苹果',
    	 'world': '世界'
    }
    print(adict['apple'])
    print(adict['world'])
    '''
    苹果
    世界
    '''
    

    如果key值不存在,会抛出异常。
    KeyError: 'xxxxx'

    3.3 新增修改元素

    使用[]可以根据key来新增/修改value。

  • 如果key不存在,对取下标操作赋值,即为新增键值对。
  • adict = {
    	 'apple': '苹果',
    	 'world': '世界'
    }
    adict['hello'] = '你好'
    print(adict)
    #{'apple': '苹果', 'world': '世界', 'hello': '你好'}
    
  • 如果key存在,则会修改键值对的值。
  • adict = {
    	 'apple': '苹果',
    	 'world': '世界'
    }
    adict['apple'] = '苹果苹果'
    print(adict)
    #{'apple': '苹果苹果', 'world': '世界'}
    

    3.4 删除元素

  • 使用pop方法根据key删除对应的键值对。
  • adict = {
    	 'apple': '苹果',
    	 'world': '世界'
    }
    adict.pop('apple')
    print(adict)
    
    # {'world': '世界'}
    

    3.5 遍历字典元素

  • 直接使用for循环能够获取到字典中的所有key,进一步就可以取出每一个值了。
  • adict = {
    	 'apple': '苹果',
    	 'world': '世界',
    	 'hello': '你好'
    }
    for key in adict:
    	print(key,adict[key])
    '''
    apple 苹果
    world 世界
    hello 你好
    '''
    

    3.6 取出所有key和value

  • 使用’keys’方法可以获取字典中的所有的key
  • adict = {
    	 'apple': '苹果',
    	 'world': '世界',
    	 'hello': '你好'
    }
    print(adict.keys())
    # dict_keys(['apple', 'world', 'hello'])
    
  • 使用values方法可以获取到字典的所有value
  • adict = {
    	 'apple': '苹果',
    	 'world': '世界',
    	 'hello': '你好'
    }
    print(adict.values())
    
    #dict_values(['苹果', '世界', '你好'])
    
  • 使用items方法可以获取字典中的所有键值对。
  • adict = {
    	 'apple': '苹果',
    	 'world': '世界',
    	 'hello': '你好'
    }
    print(adict.items())
    
    # dict_items([('apple', '苹果'), ('world', '世界'), ('hello', '你好')])
    

    3.7 合法的key类型

    不是所有的类型都可以作为字典的key的,字典的本质其实是哈希表,哈希表的key要求是可哈希的,也就是可以计算出一个哈希值。

  • 可以使用hash函数计算某个对象的哈希值。
  • 但凡能够计算出哈希值的类型,都可以作为字典的key。
  • print(hash(0))  
    print(hash(3.14))  
    print(hash('hello'))  
    print(hash(True))  
    print(hash(()))
    '''
    0
    322818021289917443
    7740487628353429172
    1
    5740354900026072187
    '''
    

    注意:

  • 列表无法计算哈希值。
  • print(hash([1,2,3,4]))
    #TypeError: unhashable type: 'list'
    
  • 字典无法计算哈希值。
  • print(hash({'a': '0'}))
    #TypeError: unhashable type: 'dict'
    

    作者:Yui_

    物联沃分享整理
    物联沃-IOTWORD物联网 » Python字典详解及功能介绍

    发表回复