Python基础总结(2)

来源:互联网 发布:mac 下载repo 编辑:程序博客网 时间:2024/06/07 04:59

字典
列表表示含义:

students=["Tom","Jim","Sue","Ann"]scores=[70,80,85,75]indexes=[0,1,2,3]name="Sue"score=0#找名字是苏的成绩是多少for i in indexes:    if students[i]=="Sue":        score=scores[i]print score

就是类似C中结构体的东西
定义字典用大括号:

scores={}scores["Jim"]=80scores["Sue"]=85scores["Ann"]=75print(scores)print(scores["Sue"])
students={    "Tim":60,    "Jim":70}print ("Tim" in students)

字典简单计数:

pantry=["apple","orange","grape","apple","orange","apple","tomato","potato","grape"]counts={}for item in pantry:    if item in counts:        counts[item]+=1    else:        counts[item]=1print counts
原创粉丝点击