字典的特性
# 1. 判断字典是否是无序的?
# python2中字典加入顺序和存储顺序不一致;
# python3中字典加入顺序和存储顺序一致;
# 字典是不支持索引的;
# 字典是不支持切片的;
# 字典的重复和连接无意义, 字典的key值是唯一的;
# 成员操作符;判断的是某个值是否为字典的key;
# print('1' in d)
# print('1' not in d)
# 字典for循环时, 默认遍历字典的key值;
for key in d:
print(key)
# 遍历字典:
for key in d:
print(key, d[key])
字典的创建
##工厂函数;
l=list([1,2,3])
print(l)
d=dict(a=1,b=2)
print(d)
##[1, 2, 3]
##{'a': 1, 'b': 2}
##字典 :key-value 值,健对应;
#value值是可以是任意数据类型: int float long complex list tuple set dict
##字典的嵌套:
student={
'1班':{
'1001':{
'name': 'MXW',
'sex':'abc'
},
'1002':{
'name':'XSD',
'sex':'cds'
}
},
'2班':{
'2001':{
'name':'sasa',
'sex':'sds'
},
'2002':{
'name':'asd',
'sex':'sx'
}
}
}
print(student['1班']['1001']['name'])
print({}.fromkeys({'1','2'},'hhhhhhh')) ##把不同的key 对应同一个value值
##MXW
##{'1': 'hhhhhhh', '2': 'hhhhhhh'}
字典的增加
services = {
'ultrabac': 1910 ,
'rhp-iibp': 1912 ,
'rhp-iibp1': 1912
}
services['http']=80
print(services)
services['http']=8080 ##若key值存在则更新对应的value值
print(services)
services['mysql']=43 ##若key值不存在则添加对应的key-value值
print(services)
#2.添加多个key-value值
services_up={
'dnf':212,
'ssa':33,
'eer':334
}
services.update(services_up)
print(services)
services.setdefault('http',9090) ##如果key值存在 ,则不做修改
print(services) ##如果key值不存在,则添加对应的key-value值
字典的删除
services = {
"http":80,
'ftp': 21,
'ssh':22,
'mysql':3306
}
#1.
del services['http']
print(services)
#2. pop删除指定key的key-value对,
#1). 如果key存在, 删除, 并且返回删除key对应的value值;
#2). 如果key不存在, 直接报错
item = services.pop('http')
print(item)
print(services)
item = services.pop('https')
print(item)
print(services)
#3. popitem删除最后一个key-value值;
item = services.popitem()
print("删除的key-value对是:", item)
print(services)
# 4. 清空字典内容;
services.clear()
print(services)
字典的查看
services = {
"http":80,
'ftp': 21,
'ssh':22,
'mysql':3306
}
# 查看字典的key值;
print(services.keys())
# 查看字典的value值;
print(services.values())
# 查看字典的(key,value)值;
print(services.items())
# 1. 查看key的value值;key不存在,则报错;
print(services['http'])
# 2. 查看key的value值;
# key不存在, 默认返回None;
# key不存在, 有default值, 则返回default值;
print(services.get('http'))
print(services.get('https'))
print(services.get('https', 443))
字典的练习
s = input("s:")
s_li = s.split()
s_dict = {}
for item in s_li:
if item in s_dict:
s_dict[item] += 1 ##key对应的value+1
else:
s_dict[item] = 1 ##将key和value=1加进去
print(s_dict)
s:ss ll lo lo ss
{‘ss’: 2, ‘ll’: 1, ‘lo’: 2}
"""
NAME : 字典的练习_随机生成卡号.py
Author: W
Date: 04/05/18
Connect: M.@qq.com
Desc:
# 1. 随机生成100个卡号;
# 卡号以6102009开头, 后面3位依次是 (001, 002, 003, 100),
# 2. 生成关于银行卡号的字典, 默认每个卡号的初始密码为"redhat";
# 3. 输出卡号和密码信息, 格式如下:
卡号 密码
6102009001 000000
"""
card_id=[] ### 存储所有卡号的列表, 也可以通过集合来存储;
for i in range(1,101):
s="6102009%.3d" %i ### %.3d代表这个整形数占3位。 eg: 1--> 001;
card_id.append(s)
card_id_info=({}.fromkeys(card_id,'redhat')) ##fromkeys()方法用于创建一个新的字典,其中包含seq的值和设置为value的值。
print("卡号\t\t\t\t\t密码")
for key in card_id_info:
print("%s\t\t\t%s" %(key,card_id_info[key]))
卡号 密码
6102009001 redhat
6102009002 redhat
6102009003 redhat
6102009004 redhat
NAME : 字典练习_随机生成数字并排序.py
Author: W
Date: 04/05/18
Connect: M.@qq.com
Desc:
# 数字重复统计:
1). 随机生成1000个整数;
2). 数字的范围[20, 100],
3). 升序输出所有不同的数字及其每个数字重复的次数;
"""
import random
# 1). 随机生成1000个整数;
all_nums = []
for item in range(1000):
# 2).数字的范围[20, 100],
all_nums.append(random.randint(20,100))
# 3). 升序输出所有不同的数字及其每个数字重复的次数;
# - 对生成的1000个数字进行排序, 然后在加入到字典中,计算个数;
sorted_nums = sorted(all_nums)
nums_dict = {}
for num in sorted_nums:
if num in nums_dict:
nums_dict[num] += 1
else:
nums_dict[num] = 1
print(nums_dict)
{20: 5, 21: 14, 22: 12, 23: 15, 24: 14, 25: 6, 26: 12, 27: 12, 28: 9, 29: 14, 30: 16, 31: 11, 32: 13, 33: 14, 34: 15, 35: 12, 36: 11, 37: 20, 38: 14, 39: 12, 40: 13, 41: 12, 42: 13, 43: 14, 44: 9, 45: 17, 46: 13, 47: 9, 48: 12, 49: 12, 50: 7, 51: 12, 52: 15, 53: 12, 54: 13, 55: 7, 56: 17, 57: 11, 58: 7, 59: 14, 60: 14, 61: 14, 62: 10, 63: 6, 64: 12, 65: 12, 66: 12, 67: 18, 68: 17, 69: 13, 70: 11, 71: 7, 72: 7, 73: 8, 74: 20, 75: 11, 76: 13, 77: 15, 78: 9, 79: 12, 80: 15, 81: 18, 82: 7, 83: 8, 84: 18, 85: 13, 86: 14, 87: 13, 88: 8, 89: 12, 90: 16, 91: 10, 92: 14, 93: 14, 94: 11, 95: 15, 96: 10, 97: 18, 98: 14, 99: 13, 100: 8}