Python知识点7——字典

本文作者:林箖霖😉
本文原创,未经允许,禁止转载!

7. Python中的字典

  1. 字典中的所有元素都是一个key-value对
  2. 字典与列表一样是一个可变序列(可进行增删改,但key为不可变对象)
  3. 以键值对的方式存储数据,字典是一个无序序列(存入的key通过hash变换)
  4. 字典是根据key查找value所在的位置
  5. 字典中不出现重复的key,但value可以重复,若在一次定义中出现两个相同的key,则最后出现的一个key类似于更新了前面相同的key的值
  6. 字典根据需要动态地伸缩,故其会浪费较大的内存,是一种空间换时间的数据结构
  7. 字典是元素可变、value可重复、key不可重复、元素无序排列的数据结构(可以进行增、删、改)

7.1 字典的创建

  1. 使用花括号 { }
   # 使用花括号
   # {key:value,key:value}
   score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十', '李六': '九十'}
   score2 = {}  # 空字典
   print(score, type(score))  # {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'} <class 'dict'>
  1. 使用内置函数dict()
   # 使用dict()函数
   # dict(key=value)
   people = dict(name='zhang', age=100, address='beijing')
   print(people, type(people))  # {'name': 'zhang', 'age': 100, 'address': 'beijing'} <class 'dict'>
  1. 使用字典生成式

    • 内置函数zip ( )
     # 内置函数zip()
     Fruits = ['apple', 'orange', 'banana']
     Numbers = [100, 200, 101]
     new_dict = zip(Fruits, Numbers)
     print(list(new_dict))  # 要转化为列表才能输出,因为zip()返回的是“由这些元组组成的列表”
    
    • for…in…
     	# 字典生成式
     	Fruits = ['apple', 'orange', 'banana']
     	Numbers = [100, 200, 101]
     	A = {Fruits: Numbers for Fruits, Numbers in zip(Fruits, Numbers)}
     	print(A)
    	 # 如果两个字典中的元素不相等,则以短的字典为主
     	Tools = ['rule', 'desk', 'pen']
    	 Prices = [100, 10, 20, 30, 102]
    	 B = {Tools: Prices for Tools, Prices in zip(Tools, Prices)}
    	 print(B)  # {'rule': 100, 'desk': 10, 'pen': 20}
    

7.2 字典元素的获取

  1. 使用[ ]
   # 使用[]
   score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'}
   print(score['张三'])  # 通过key值查询相应的value值  100
   # print(score['zhang'])  # 查找的key值不存在,抛出 KeyError: 'zhang'
  1. 使用get( )
   # 使用get(),get(key,self)
   print(score.get('李四'))  # 通过key值查询相应的value值 90
   print(score.get('zhang', 99))  # 查找相应的键值不存在,返回默认值99
   print(score.get('zhang'))  # 查找相应的键值不存在,返回None

7.3 字典元素的判断

  1. key的判断
   # in / not in
   score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'}
   print('zhang' in score)  # False
   print('张三' not in score)  # False

7.4 字典元素的增删改

  1. 字典元素的增加
   # 字典元素的添加
   score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'}
   score['成七'] = 88
   print(score)  # {'张三': 100, '李四': 90, '王五': 80, '李六': '九十', '成七': 88}
  1. 字典元素的删除

    • del
       # 字典元素的删除
       score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'}
       del score['张三']  # 通过指定key删除键值对
       print(score)  # {'李四': 90, '王五': 80, '李六': '九十'}
    
    • clear ( )
       # 清空字典元素
       score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'}
       score.clear()
       print(score)  # {}
    
  2. 字典元素的修改

   # 字典元素的修改
   score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'}
   score['成七'] = 400
   print(score)  # {'张三': 100, '李四': 90, '王五': 80, '李六': '九十', '成七': 400}

7.5 字典视图的获取

  1. key ( )

       # 获取字典中的所有key
       score = {'张三': 100, '李四': 90, '王五': 80, '李六': 100}
       print(score.keys())  # dict_keys(['张三', '李四', '王五', '李六'])
       print(type(score.keys()))  # <class 'dict_keys'>
       # 将key转化成列表
       print(list(score.keys()))  # ['张三', '李四', '王五', '李六']
    
  2. value ( )

    # 获取字典中的所有value
    score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'}
    print(score.values())  # dict_values([100, 90, 80, '九十'])
    print(type(score.values()))  # <class 'dict_values'>
    # 将value转化成列表
    print(list(score.values()))  # [100, 90, 80, '九十']
    
  3. items ( )

    # 获取字典中的所有键值对
    score = {'张三': 100, '李四': 90, '王五': 80, '李六': 100}
    print(score.items())  # dict_items([('张三', 100), ('李四', 90), ('王五', 80), ('李六', 100)])
    print(type(score.items()))  # <class 'dict_items'>
    # 将键值对转化为列表
    print(list(score.items()))  # [('张三', 100), ('李四', 90), ('王五', 80), ('李六', 100)]
    

7.6 字典元素的遍历

# 字典元素的遍历
score = {'张三': 100, '李四': 90, '王五': 80, '李六': 100}
for i in score:
    print(i)  # 获取key
    print(score[i])  # 获取values
    print(score.get(i))  # 获取values
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值