python字典小知识

                  字典的小知识
dic = {"name": "tom", "age": 23, "price": 110}
# 01:提取键值
  
print("1>>", dic["name"],type(dic["name"]))  # 1>> tom    <class 'str'>
print("2>>", dic.get("name")) # 1>> tom
print("3>>", dic.get("n"),type(dic.get("n"))) # 3>> None <class 'NoneType'>
# 02:判断键或值是否存在
  
 print('name' in dic)   #True
print('tom' in dic) #False
print('n' in dic) #False
# 03:设定指定字段值
  
dic['name']="jack"
print(dic) #{'name': 'jack', 'age': 23, 'price': 110}
print(dic['name']) # jack
# 04:访问所有键和值
  
# 1:提取所有的键
print(dic.keys()) # dict_keys(['name', 'age', 'price'])
print(type(dic.keys())) # <class 'dict_keys'> # 2:提取所有的值
print(dic.values()) # dict_values(['tom', 23, 110])
print(type(dic.values())) # <class 'dict_values'> for key, value in dic.items():
print(">>", key, value) >> name tom
>> age 23
>> price 110

 
上一篇:SQL 常用方法


下一篇:vue10行代码实现上拉翻页加载更多数据,纯手写js实现下拉刷新上拉翻页不引用任何第三方插件