当前位置 博文首页 > sunjianxi1996的博客:python学习笔记——字典生成式
items = [‘Fruits’, ‘Books’, ‘Others’]
prices = [100, 96, 98, 150] # 只会匹配三个
d = {item: price for item, price in zip(items, prices)}
print(d)
‘’’
items = {‘Fruits’, ‘Books’, ‘Others’}
prices = {100, 96, 98, 150} # 以少的为基准,只会匹配三个
d = {item: price for item, price in zip(items, prices)}
print(d)
‘’’
‘’’
总结:
1.字典的创建:
{}
内置函数dict()
字典生成式
2.常用操作
获取value
字典名key
字典名.get(key)
删除key-value对
del 字典名[key]
修改/新增
字典名[key] = value
in / not in
‘’’