- 字典的定义
#字典是一个无序的数据集合,使用print输出字典的时候
#通常输出的顺序和定义的顺序是不一致的
字典里key的值对应value值
users = ['user1','user2']
passwd = ['123','456']
print(zip(users,passwd)
print(list(zip(users,passwd)))
print(dict(zip(users,passwd)))
运行结果为:
<zip object at 0x7fb5588a0948>
[('user1', '123'), ('user2', '456')]
{'user1': '123', 'user2': '456'}
空字典:
s = {}
print(type(s))
运行结果为:
<class 'dict'>
字典:key - value 键值对
#value可以是任意数据类型
s = {
'linux':[100,90,89],
'redhat':[89,99,79]
}
print(s,type(s))
运行结果为:
{'linux': [100, 90, 89], 'redhat': [89, 99, 79]} <class 'dict'>
- 工厂函数
d = dict()
print(type(d))
运行结果为:
<class 'dict'>
d1 = dict(a=1,b=2)
print(d1,type(d1))
运行结果为:
{'a': 1, 'b': 2} <class 'dict'>
- 字典的嵌套
students = {
'25061602':{
'name':'jiachen',
'age':18,
'score':100
},
'25061629':{
'name':'youxiu',
'age':13,
'score':99
}
}
print(students['25061602']['name'])
运行结果为:
jiachen
#所有的key和value值是一样的
字典的顺序是随机的
print({}.fromkeys({'1','2','3'},'00000'))
运行结果为:
{'3': '00000', '2': '00000', '1': '00000'}
-
字典的特性
-
字典不支持索引
print(d[0])
- 字典不支持切片
- 字典的重复和连接无意义
- 成员操作符
d = {
'1':'a',
'2':'b'
}
print('1' in d)
运行结果为:
True
- for循环,默认遍历字典的key值
for i in d:
print(i)
运行结果为:
1
2
- 遍历字典
for i in d:
print(i,d[i])
运行结果为:
1 a
2 b
- 字典的增加
services = {
‘http’:80,
‘ftp’:21,
‘ssh’:22
}
增加一个元素
#如果key值存在,则更新对应的value值
#如果key值不存在,则添加对应key-value
services['mysql'] = 3306
print(services)
运行结果为:
{'http': 80, 'ftp': 21, 'ssh': 22, 'mysql': 3306}
services['http'] = 443
print(services)
运行结果为:
{'http': 443, 'ftp': 21, 'ssh': 22}
#添加多个key-value值
services_backup = {
'https':443,
'tomcat':8080,
'http':8080
}
运行结果为:
{'http': 8080, 'ftp': 21, 'ssh': 22,'https':443,'tomcat':8080}
services.update(services_backup)
print(services)
运行结果为:
{'http': 8080, 'ftp': 21, 'ssh': 22,'https':443,'tomcat':8080}
services.update(flask=9000,http=8000)
print(services)
运行结果为:
{'http': 8000, 'ftp': 21, 'ssh': 22,'https':443,'tomcat':8080}
setdefault添加key值
#如果key值存在,不做修改
#如果key值不存在,添加对应的key-value
services.setdefault('http',9090)
print(services)
运行结果为:
{'http': 80, 'ftp': 21, 'ssh': 22}
services.setdefault('oracle',44575)
print(services)
运行结果为:
{'http': 80, 'ftp': 21, 'ssh': 22,'oracle':44575}
- 字典的删除
services = {
'http':80,
'ftp':21,
'ssh':22
}
del services['http']
print(services)
运行结果为:
{'ftp': 21, 'ssh': 22}
#pop删除指定key的key-value
#如果key存在,删除,并返回删除key对应的value
#如果不存在,报错
item = services.pop('http')
print(item)
print(services)
运行结果为:80
{'ftp': 21, 'ssh': 22}
- popitem删除最后一个key-value值
item = services.popitem()
print('删除的是:',item)
print(services)
运行结果为:
删除的是:('ssh':22)
{'http':80,'ftp':21}
- 清空字典内容
services.clear()
print(services)
运行结果为:
{}
- 字典的查看
services = {
'http':80,
'ftp':21,
'ssh':22
}
- 查看字典的key值
print(services.keys())
运行的结果为:
dict_keys(['http','ftp','ssh'])
- 查看字典的value值
print(services.values())
运行的结果为:
dict_values(['80','21','22'])
- 查看字典的key-value值
print(services.items())
运行的结果为:
dict_items([('http',80),('ftp',21)('ssh',22)])
- #查看key的value值
#key不存在,默认返回none
#key不存在,有default值,则返回default值
print(services.get('http'))
运行的结果为:
None
- 遍历
for k,v in services.items():
print(k,'--->',v)
for k in services:
print(k,'--->',services[k])
运行结果为:
http ---> 80
ftp ---> 21
ssh ---> 22
get方法获取指定key对应的value
print(services.get('https','key not exist'))
运行结果为:key not exist
- 字典练习题
例题1: 重复的单词: 此处认为单词之间以空格为分隔符, 并且不包含,和.>;
#1. 用户输入一句英文句子;
- 打印出每个单词及其重复的次数;
“hello java hello python”
hello 2
java 1
python 1
s = input('s:')
#1.把每个单词分割处理
s_li = s.split()
print(s_li)
#通过字典存储该单词和其出现的次数
word_dict = {}
"""
依次循环遍历列表
如果列表元素不在字典的key中,将元素作为key 1作为value值
如果列表元素在字典的key中,直接更新元素的value值,在原有的基础上加1
"""
for item in s_li:
if item not in word_dict:
word_dict[item] = 1
else:
word_dict[item] += 1
print(word_dict)
例题2:
数字重复统计:
- 随机生成1000个整数
- 数字范围[20,100]
- 升序输出所有不同的数字及其每个数字的重复次数