数据类型的划分:可变数据类型和不可变数据类型。
不可变数据类型(可哈希):元祖、bool、int、str
可变数据类型(不可哈希):list、dict,set(集合)
dict(字典):
dict(字典):映射数据类型
dict = {"key":value}
dict key 必须不可变数据类型,可哈希。
value 任意数据类型
dict 优点:二分法查找去查询
存储大量的关系型数据
特点:无序的(3.5及其之前的版本)
# dict = {
# 'name':['韩非','黄有华','林颖','谢龙','李霞霞','纪伟鹏'],
# '配送':[{'num':12,'avera_age':20}],
# True:1,
# (1,2,3):'wuyiyi',
# 2:'二哥',
# }
# print(dict)
#例子
# dict1 = {'age':18,'name':'jone','sex':'male'}
#增 dict['key']:value 或 setdefault(key,value)
# dict1['hight']=185 #没有键值对,添加
# dict1['age'] = 16 #如果有键值对,则值覆盖
# print("dict1['hight']=185 and dict1['age']=16",dict1)
# 输出结果:
# dict1['hight']=185 and dict1['age']=16 {'hight': 185, 'sex': 'male', 'age': 16, 'name': 'jone'}
#setdefault() 已经存在键值对,不做任何改变,没有在添加
# dict1.setdefault('weight',150)
# dict1.setdefault('name','alex')
# print(dict1)
# 输出结果:
# dict1['hight']=185 and dict1['age']=16 {'hight': 185, 'sex': 'male', 'age': 16, 'name': 'jone'}
#删 pop(key,'设置返回值')、popitem(key)随机删除,有返回值、del dict[key] 不存在会报错、clear()清空字典
#ps:None是一种数据类型
# print(dict1.pop('age'),dict1)
# print(dict1.pop('yijiajun',None),dict1)
# 输出结果:
# 18 {'name': 'jone', 'sex': 'male'}
# None {'name': 'jone', 'sex': 'male'}
# print(dict1.popitem(),dict1)
# 输出结果:
# ('name', 'jone') {'sex': 'male', 'age': 18}
# del dict1['age']
# print(dict1)
# 输出结果:
# {'name': 'jone', 'sex': 'male'}
# del dict1
# print(dict1)
# 输出结果:
# print(dict1)
# NameError: name 'dict1' is not defined
#改
# dict1['age'] = 20
# print(dict1)
# 输出结果:
# {'name': 'jone', 'sex': 'male', 'age': 20}
#update() 已经存在的键值对,会更新value,不存在的键值对,则添加
# dict2 = {'name':'yijiajun','heigh':175,'weight':75}
# dict1.update(dict2)
# print(dict1,dict2)
#查
# print(dict1.keys(),type(dict1.keys()))
# print(dict1.values(),type(dict1.values()))
# print(dict1.items(),type(dict1.items()))
# print(dict1.get('name'),type(dict1.get('name')))
# print(dict1.get('yijia'),'不存在key')
# 输出结果:
# dict_values([18, 'male', 'jone']) <class 'dict_values'>
# dict_items([('age', 18), ('sex', 'male'), ('name', 'jone')]) <class 'dict_items'>
# jone <class 'str'>
# None 不存在key
#for .....in.....遍历任何项目的序列
#遍历key
#方法一
# for i in dict1.keys():
# print(i)
#方法二
# for i in dict1:
# print(i)
#遍历value
# for i in dict1.values():
# print(i)
#以元祖的形式同时遍历key和value
# for i in dict1.items():
# print(i,type(i))
# 输出结果:
# ('sex', 'male') < class 'tuple'>
# ('name', 'jone') <class 'tuple'>
# ('age', 18) < class 'tuple'>
#以字符串的形式同时遍历key和value
# for k,v in dict1.items():
# print(k,v,type(k),type(v))
# 输出结果:
# dict_values([18, 'male', 'jone']) <class 'dict_values'>
# dict_items([('age', 18), ('sex', 'male'), ('name', 'jone')]) <class 'dict_items'>
# jone <class 'str'>
# None 不存在key
#字典嵌套
# dic = {
# 'name':['yijiajun','yijiaping','yijiaxing'],
# 'home.yee':{
# 'born' :1995,
# 'count':85,
# 'old':'grandfather'
# },
# True:1,
# (1,2,3):'yimeiling',
# 'age':21
#
# }
# dic['age'] = 56
# dic['name'][0] = dic['name'][0].upper()
# dic['name'].append('wangxizhi')
# dic['home.yee']['color'] = 'red'
# dic['home.yee'].setdefault('like','fish')
# dic['home.yee'].pop('born','已经删除')
# print(dic['home.yee'].pop('born','已经删除'))
#课间题 统计数字出现的次数(连续的数字为1个)
info = input('请输入您要检测的数字:').strip()
for i in info:
if i.isalpha():
info = info.replace(i,' ')
l = info.split( )
print(l)
print(len(l))