小白的python之旅(八)----字典的操作
"""
字典(dic)是容器型数据类型,将{}作为容器的标志,多个元素用逗号隔开,字典元素都是键值对
{键1:值1,键2:值2,......} 键就是关键字 值就是数据
字典是可变的,支持增删改;字典是无序的(不支持下标操作)
键 ---> 不能是可变的数据;唯一 一般都是字符串
值 ---> 任何类型的数据,可以重复
键是用来对值进行区分和说明
如果存在重复的key,后一个key对应的value会覆盖前一个key对应的value
"""
stu = {'name': 'jack', 'age': 18, 'score': 90, 'id': '201531070363'}
print(stu['name'])
print(stu['score'])
print(stu.get('age'))
print(stu.get('height'))
print(stu.get('id', '201531070367'))
print(stu.get('height', '175'))
print('=======================')
for key in stu:
print(key, end=' ')
print()
print('=======================')
for key in stu:
print(stu[key], end=' ')
print()
print('=======================')
for value in stu.values():
print(value, end=' ')
print()
print('=======================')
for key in stu.keys():
print(key, end=' ')
print()
print('=======================')
for key, value in stu.items():
print(key, value, end=' ')
print()
print('=======================')
""" 字典的增加和修改 """
stu1 = {'name': 'jack', 'age': 18, 'score': 90, 'id': '201531070363'}
stu1['name'] = 'rose'
print(stu1)
stu1['height'] = '1.75'
print(stu1)
stu1.setdefault('name', 'mike')
print(stu1)
stu1.setdefault('weight', '50kg')
print(stu1)
""" 字典的删除 """
stu2 = {'name': 'rose', 'age': 18, 'score': 90, 'id': '201531070363', 'height': '1.75', 'weight': '50kg'}
del stu2['weight']
print(stu2)
height1 = stu2.pop('height')
print(height1)
print(stu2)
students = {'001': {'name': 'jack', 'age': 18, 'score': 90, 'id': '201531070363'},
'002': {'name': 'Rose', 'age': 20, 'score': 77, 'id': '201531070362'},
'003': {'name': 'Mike', 'age': 17, 'score': 94, 'id': '201531070365'}
}
print(students['001']['id'])
print('=======================')
students_list = []
i = 0
while i < 4:
students_dic = {}
name = input('请输入您的姓名')
students_dic['name'] = name
age = int(input('请输入您的年龄'))
students_dic['age'] = age
tel = input('请输入您的电话')
students_dic['tel'] = tel
score = int(input('请输入您的成绩'))
students_dic['score'] = score
students_list.append(students_dic)
i += 1
print(students_list)
count = 0
for x in students_list:
if x['age'] < 18:
count += 1
print('未成年人的个数为{}'.format(count))
字典的相关方法
stu3 = {'name': 'rose', 'age': 18, 'score': 90, 'id': '201531070363', 'height': '1.75'}
stu4 = {'age': 18, 'name': 'rose', 'score': 90, 'height': '1.75', 'id': '201531070363'}
print(stu3 == stu4)
print('name' in stu4)
print('rose' in stu4)
print(len(stu4))
list1 = [['age', 20], ['name', 'lili']]
dict1 = dict(list1)
print(dict1)
x = ('we', 'rg', 'dg')
dict2 = dict(x)
print(dict2)
print(list(stu4), tuple(stu4))
dict3 = dict.fromkeys('abc')
print(dict3)
dict4 = dict.fromkeys(['name', 'age', 'tel', 'gender'])
print(dict4)
dict5 = dict.fromkeys(['name', 'age', 'tel', 'gender'], 'iii')
print(dict5)
"""
字典.keys() 获取字典所有的key,返回一个容器,不是列表
字典.values() 获取字典所有的value,返回一个容器,不是列表
字典.items() 获取字典所有的key 和 value,返回一个容器,容器中的元素是一个元组,每一个元组是一个键值对
字典.popitem() 去除最后一个键值对
字典.update(序列) 是将序列转换为字典,用转换后的字典去更新指定的字典 ,不存在就添加,存在就修改
"""
dict6 = {'name': 'jack'}
dict5.update(dict6)
print(dict5)
dict7 = {'height': '1.78'}
dict5.update(dict7)
print(dict5)