字典的学习笔记

  • 列表  []     单身
  • 什么是字典  {}  二人世界

python内置的数据结构之一,与列表一样是一个可变序列(可以增删改操作的)
以键值对的方式存储数据,字典是一个无序的序列  -> hash(key)  通过哈希函数来计算存储位置,key一定是不可变的
字典的创建
使用花括号:  score={'张三':100,'李四':98,'王五':55}
第二种创建方式:使用内置函数   dict()
 


print('使用花括号创建')
score = {'张三': 100, '李四': 98, '王五': 55}
print(score, id(score))
print(type(score))
print('第二种创建方式:使用内置函数')
student = dict(name='liu', age=34)
print(student)
'''空字典'''
dit = {}
print(dit, type(dit))

print('=========获取字典中的值=======')
print('使用花括号创建')
score = {'张三': 100, '李四': 98, '王五': 55}

print(score['张三'])
# print(score['张1']) #如果查找的键不存在 会报错,KeyError: '张1'
print(score.get('张三'))
print(score.get('张1'))  # None  ,如果查找的键不存在
print(score.get('张1', 99))  # 如果查找的键张1不存在,给返回一个默认值

print('=======key的判断========')
print('张三' in score)
print('张三' not in score)

del score['张三']  # 删除指定的key-value对
print(score, id(score))
# score.clear()
print(score, id(score))
score['程力'] = 93
print(score)
score['程力'] = 95
print(score)

print('======获取所有的key========')
score = {'张三': 100, '李四': 98, '王五': 55}
keys = score.keys()
print(keys)  # dict_keys(['张三', '李四', '王五'])
print(type(keys))  # <class 'dict_keys'>
print(list(keys))  # ['张三', '李四', '王五'] 将所有建组成的视图转成列表

print('======获取所有的value========')
values = score.values()
print(values)  # dict_values([100, 98, 55])
print(type(values))  # <class 'dict_values'>
print(list(values))

print('======获取所有的key-value对========')
items = score.items()
print(items)  # dict_items([('张三', 100), ('李四', 98), ('王五', 55)]) ,转换之后的元素由元组组成-》 元组()

print('字典元素的遍历')
for i in score:
    print(i)  # 输出所有的列表中所有的键 key
    print(score[i], score.get(i))  # 获取键对应的值
print('字典元素的key不允许重复')
d = {'name': '张三', 'name': '李四'}  # key不允许重复,一旦重复出现值覆盖的情况
print(d)  # {'name': '李四'}

d = {'name': '张三', 'nick_name': '张三'}
print(d)

lst = [1, 2, 3, 4]
lst.insert(1, 100)
print(lst)

# d = {lst: 100}
# print(d)  # 可变对象不可以作为key  TypeError: unhashable type: 'list'

print('====================')

items = ['Fruits', 'books', 'Others']
prices = [85, 23, 52]
d = {i: prices for i, prices in zip(items, prices)}
print(d)

items = ['Fruits', 'books', 'Others']
prices = [85, 23, 52,30,40] #更长5个元素,但是只有前3个元素有效
d = {i: prices for i, prices in zip(items, prices)}#以元素少的那个列表来生成
print(d)

lst = ['张三', '李四', '王五']
score = [12, 23, 34, 45, 56]
item = zip(lst, score)
print(item)  # <zip object at 0x000001A9FAC29408>
print('=================')
print(list(item))  # [('张三', 12), ('李四', 23), ('王五', 34)]

lst = ['Fruits', 'Books', 'Carrit']
dit = {i.upper(): score for i, score in zip(lst, score)}
print(type(dit)) #<class 'dict'>
print(dit)    #{'FRUITS': 12, 'BOOKS': 23, 'CARRIT': 34}

下面为扩展阅读:

1. 直接创建空字典

dic = {}
print(type(dic))
# 输出结果:<class 'dict'>

2. 直接赋值创建字典

dic = {'name': 'Jack', 'age': 18, 'height': 180}
print(dic)
# 输出结果:{'name': 'Jack', 'age': 18, 'height': 180}

3. 通过关键字dict和关键字参数创建

dic = dict(name='Jack', age=18, height=180)
print(dic)
# 输出结果:{'name': 'Jack', 'age': 18, 'height': 180}

实例:

       •输出一个类似{ i : i*i }的字典

dic = dict()
for i in range(1, 5):
    dic[i] = i * i
print(dic)
# 输出结果:{1: 1, 2: 4, 3: 9, 4: 16}

4. 通过关键字dict和二元组列表创建

lis = [('name', 'Jack'), ('age', 18), ('height', 180)]
dic = dict(lis)
print(dic)
# 输出结果:{'name': 'Jack', 'age': 18, 'height': 180}

5. 通过关键字dict和zip创建

dic = dict(zip('abc', [1, 2, 3]))
print(dic)
# 输出结果:{'a': 1, 'b': 2, 'c': 3}

 6. 通过字典推导式创建

dic = {i: i ** 2 for i in range(1, 5)}
print(dic)
# 输出结果:{1: 1, 2: 4, 3: 9, 4: 16}

 7. 通过dict.fromkeys()创建

注意:通常用来初始化字典, 设置value的默认值

dic = dict.fromkeys(range(4), 'x')
print(dic)
# 输出结果:{0: 'x', 1: 'x', 2: 'x', 3: 'x'}


参考链接:https://blog.csdn.net/qq_45261963/article/details/108936881

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aFakeProgramer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值