概述
chars = ['a','c','d','s','a','c','d','s','c']
char_count = {}
for char in chars:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
#if char not in char_count:
#char_count[char] = charss.count(char)
print(char_count)
"""*********************************************"""
#让用户输入姓名,判断是否存在,若不存在,则添加到列表里
persons = [
{'name':'zhangsan','age':18,},
{'name':'lisi','age':'20'},
{'name':'wangwu','age':'19'},
{'name':'tom','age':'21'},
]
x = input('请输入你的姓名:')
for person in persons:
#if name in person: #in运算符,如果直接用在字典里,是用来判断key是否存在
if person['name'] == x:
print('您输入的名字已存在')
break
else:
#print('您输入的名字不存在')
#创建一个新的字典 new_person
new_person = {'name':x}
y = int(input('请输入您的年龄:'))
new_person['age'] = y
#把这个新的数据存储到person列表里
persons.append(new_person)
print('用户添加成功')
confusion = {}
confusion[1] = 1
confusion['1'] = 2
confusion[1] += 1
sum = 0
for k in confusion:
sum += confusion[k]
print(sum)
###################################################
dict1 = {'A':'100','B':'200','C':'300'}
dict2 = {}
for k,v in dict1.items():
dict2[v] = k
dict1 = dict2
# dict1 = {v:k for k,v in dict1.items()} 字典推导式
print(dict1)
结果
{'a': 2, 'c': 3, 'd': 2, 's': 2}
请输入你的姓名:curry
请输入您的年龄:30
用户添加成功
4
{'100': 'A', '200': 'B', '300': 'C'}
Process finished with exit code 0
#声明一个字典保存学生信息(姓名,年龄,成绩,电话,性别...)
Student = {'name':'curry','sex':'male','age':'30','tel':'123456789','score':'100'}
#声明一个列表,里面包含6个字典
student1 = [
{'name':'curry','sex':'male','age':15,'tel':'13545678914','score':10},
{'name':'tom','sex':'female','age':20,'tel':'16256852178','score':70},
{'name':'jimmy','sex':'female','age':13,'tel':'16415667','score':50},
{'name':'jerry','sex':'male','age':16,'tel':'193456642','score':80},
{'name':'chirs','sex':'female','age':20,'tel':'158310567230','score':66},
{'name':'culi','sex':'male','age':24,'tel':'13341902089','score':40},
]
#1.统计不及格的人数
count = 0
teenage_count = 0
for student in student1:
if student['score'] < 60:
count += 1
print('不及格的学生有%d个'%count)
#2.打印不及格的学生和对应的成绩
for student in student1:
if student['score'] < 60:
print('%s不及格,分数是%d'%(student['name'],student['score']))
print(student)
if student['age'] < 18:
teenage_count += 1
if student['tel'].endswith('9'):
#if student['tel'][-1] == 9:
print('%s的手机号以9结尾'%student['name'])
print('不及格的学生有%d个'%count)
print('未成年的学生有%s个'%teenage_count)
结果
不及格的学生有3个
curry不及格,分数是10
{'name': 'curry', 'sex': 'male', 'age': 15, 'tel': '13545678914', 'score': 10}
jimmy不及格,分数是50
{'name': 'jimmy', 'sex': 'female', 'age': 13, 'tel': '16415667', 'score': 50}
culi不及格,分数是40
{'name': 'culi', 'sex': 'male', 'age': 24, 'tel': '13341902089', 'score': 40}
culi的手机号以9结尾
不及格的学生有3个
未成年的学生有3个
Process finished with exit code 0
最后
以上就是天真美女为你收集整理的Python笔记-字典的练习的全部内容,希望文章能够帮你解决Python笔记-字典的练习所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复