
# 字典
score={"张三":"23","王五":"45"}
print(score)
dct=dict(name='张三',age='20')
print(dict)
print(type(score))
# 字典元素的获取
print(score['张三'])
print(score.get('张三'))
# 判断是否是字典中的元素
print('王五' in score)
# 为字段字段赋值/新增
score['张三']='56'
print(score)
# 字典元素删除
del score['张三']
# 遍历字典中的元素keys values items
for k,v in score.items():
print(k,v)
# 生成字典
items=['张']
price= ['29']
new_dict={item.upper() for item,pric in zip(items,price)}
print(new_dict)
# 清空字典
# score.clear()
本文详细介绍了Python中字典的数据结构及其常用操作,包括创建、访问、修改和删除字典元素,以及字典的合并与遍历等核心知识点,帮助开发者更好地理解和运用Python字典。
1万+

被折叠的 条评论
为什么被折叠?



