'''
字典是另一种可变容器模型,且可存储任意类型对象。
字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:
dict = {key1 : value1, key2 : value2 }
'''
'''
info = {'name':'班长', 'id':100, 'sex':'f', 'address':'地球亚洲中国北京'}
说明:
字典和列表一样,也能够存储多个数据
列表中找某个元素时,是根据下标进行的
字典中找某个元素时,是根据'名字'(就是冒号:前面的那个值,例如上面代码中的'name'、'id'、'sex')
字典的每个元素由2部分组成,键:值。例如 'name':'班长' ,'name'为键,'班长'为值
'''
infodict = {'name':'张三', 'id':100, 'sex':'m', 'address':'中国北京','age':'20','born':'1999','score':'666'}
print(infodict['name'])
print(infodict['id'])
print(infodict['address'])
print(infodict['sex'])
print(infodict['score'])
print(infodict['age'])
print(infodict['born'])
'''
张三
100
中国北京
m
666
20
1999
'''
'''
报错:
Traceback (most recent call last):
File "e:/Basic/dict.py", line 33, in
print(infodict['ages'])
KeyError: 'ages'
'''
ages = infodict.get('ages')
Type = type(ages)
print(Type)
ages = infodict.get('ages',112233)
print('ages=',ages)
'''
1.查看字典元素
'''
print(infodict['name'])
print(infodict.get('id'))
print(infodict.get('idd'))
'''
2.修改字典元素
'''
info = {'name':'张三', 'id':100, 'sex':'m', 'address':'中国北京','age':'20','born':'1999','score':'666'}
newId = input('请输入新的学号:')
info['id'] = int(newId)
print('修改之后的id为%d:'%info['id'])
dict1 = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
dict1['Age'] = 8
dict1['School'] = "菜鸟教程"
print ("dict['Age']: ", dict1['Age'])
print ("dict['School']: ", dict1['School'])
'''
请输入新的学号:666
修改之后的id为666:
'''
'''
3.添加元素
'''
infoadd = {'name':'班长', 'sex':'f', 'address':'中国北京'}
newborn= input('请输入新的学号')
infoadd['newborn'] = newborn
print('添加之后的newborn为:%s'%infoadd['newborn'])
'''
4.删除元素
'''
infodel = {'name':'张三', 'id':100, 'sex':'m', 'address':'中国北京','age':'20','born':'1999','score':'666'}
print('删除前,%s'%infodel['name'])
print('删除前,%s'%infodel)
print('删除后,%s'%infodel)
'''
删除前,张三
删除前,{'name': '张三', 'id': 100, 'sex': 'm', 'address': '中国北京', 'age': '20', 'born': '1999', 'score': '666'}
Traceback (most recent call last):
File "e:/Basic/dict.py", line 121, in
print('删除后,%s'%infodel)
NameError: name 'infodel' is not defined
'''
infoclear = {'name':'张三', 'id':100, 'sex':'m', 'address':'中国北京','age':'20','born':'1999','score':'666'}
print('clear 清除之前:',infoclear)
infoclear.clear()
print('clear 清除之后:',infoclear)
'''
clear 清除之前: {'name': '张三', 'id': 100, 'sex': 'm', 'address': '中国北京', 'age': '20', 'born': '1999', 'score': '666'}
clear 清除之后: {}
'''
'''
字典键的特性
字典值可以是任何的 python 对象,既可以是标准的对象,也可以是用户定义的,但键不行。
两个重要的点需要记住:
1)不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住,如下实例:
实例
#!/usr/bin/python3
dict = {'Name': 'Runoob', 'Age': 7, 'Name': '小菜鸟'}
print("dict['Name']: ", dict['Name'])
以上实例输出结果:
dict['Name']: 小菜鸟
2)键必须不可变,所以可以用数字,字符串或元组充当,而用列表就不行,如下实例:
实例
#!/usr/bin/python3
dict = {['Name']: 'Runoob', 'Age': 7}
print("dict['Name']: ", dict['Name'])
以上实例输出结果:
Traceback (most recent call last):
File "test.py", line 3, in
dict = {['Name']: 'Runoob', 'Age': 7}
TypeError: unhashable type: 'list'
'''
infolen = {'name':'张三', 'id':100, 'sex':'m', 'address':'中国北京','age':'20','born':'1999','score':'666'}
print('字典元素(键)个数:',len(infolen))
dict_to_str = str(infolen)
print(dict_to_str)
'''
dict.clear() 删除
'''
dictclear = {'name':'张三', 'id':100, 'sex':'m', 'address':'中国北京','age':'20','born':'1999','score':'666'}
print ("字典长度 : %d" % len(dictclear))
dictclear.clear()
print ("字典删除后长度 : %d" % len(dictclear))
"""
字典长度 : 7
字典删除后长度 : 0
"""
dictclen = {'name':'张三', 'id':100, 'sex':'m', 'address':'中国北京','age':'20','born':'1999','score':'666'}
dict_len = len(dictclen)
print(dict_len)
dictkey = dictclen
dict_key = dictkey.keys()
print(dict_key)
dictvalues = dictkey
dict_values = dictvalues.values()
print(dict_values)
dictitems = dictkey
dict_items = dictitems.items()
print(dict_items)
'''
dict_keys(['name', 'id', 'sex', 'address', 'age', 'born', 'score'])
dict_values(['张三', 100, 'm', '中国北京', '20', '1999', '666'])
dict_items([('name', '张三'), ('id', 100), ('sex', 'm'), ('address', '中国北京'), ('age', '20'), ('born', '1999'), ('score', '666')])
'''
'''
dict.copy()
函数返回一个字典的浅复制。
'''
dict1 = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
dict2 = dict1.copy()
print ("新复制的字典为 : ",dict2)
'''
新复制的字典为 : {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
'''
'''
fromkeys() 函数用于创建一个新字典,以序列 seq 中元素做字典的键,value 为字典所有键对应的初始值。
dict.fromkeys(seq[, value])
seq -- 字典键值列表。
value -- 可选参数, 设置键序列(seq)对应的值,默认为 None。
返回值
该方法返回一个新字典。
'''
import random
offices = [[],[],[]]
names = ['A','B','C','D','E','F','G','H']
i = 0
for name in names:
index = random.randint(0,2)
offices[index].append(name)
i = 1
for tempNames in offices:
print('办公室%d的人数为:%d'%(i,len(tempNames)))
i+=1
for name in tempNames:
print("%s"%name,end='')
print("\n")
print("-"*20)
'''
# 练习题
x = True
country_counter = {}
def addone(country):
if country in country_counter:
country_counter[country] += 1
else:
country_counter[country] = 1
addone('China')
addone('Japan')
addone('china')
print(len(country_counter))
# 3
confusion = {}
confusion[1] = 1
confusion['1'] = 2
confusion[1] += 1
sum = 0
for k in confusion:
sum += confusion[k]
print(sum)
# 4
'''