python 第五天:字典的基本操作

'''字典键值的插入'''
zhangxu = {'height':170,'weight':70 }
zhangxu["dick's_length"] = 0.5
zhangxu["hair_cut"] = "san dun"
print (zhangxu)
print ("Where did Zhang Xu cut his hair:")
print (zhangxu["hair_cut"].title())

'''items()、keys()、values()的应用'''
exam_score = {
   'zhang xu' : 83,
   'gu er cheng':41,
   'wu you' :64,
   'pang bi chi' :88,
   'tang jun' :77,
   }
top_student = ['gu er cheng','zhang xu']
sum = 0
average = 0
for name,score in exam_score.items():
    sum = score+sum
    print ("Student "+name.title()+" got "+str(score)+" points!")
print("=====================================================================")
print ("The average points in this exam are: ",sum/len(exam_score))
print("=====================================================================")
for name in exam_score.keys():
    if name in top_student:
 print ("How stupid are you "+name.upper()+" you just got "+str(exam_score[name])+" points!")
    else:
        print ("Good job,boy!"+name.title())
print ("########################################################################")
top_points =[]
normal_points=[]
shitty_points=[]
for point in exam_score.values():
    if point > 80:
        top_points.append(point)
    elif point >60 and point <80:
        normal_points.append(point)
    else:
        shitty_points.append(point)
print ("top points are:",top_points)
print ("normal points are:",normal_points)
print ("shitty points are:",shitty_points)

'''对之前学到的sorted函数的进一步研究'''
points = [83,-41,54,-22,-71,99,-95,3,-11]
print(sorted (points,key = lambda x:abs(x),reverse=True))
'''lambda 为无名函数,即没必要特别指出一个函数的时候做函数替代用的,本例实现的是对一组数按他们绝对值从大到小排列'''

猜你喜欢

转载自blog.csdn.net/CalvinHARRIS/article/details/82777338