我将如何使用字符串和拆分来从列表中三个特定人的三个不同等级中获得某个等级。这是清单。Justin:Calculus$90:Java$85:Python$88:Taylor:Calculus$73:Java$95:Python$86:Drew:Calculus$80:Java$75:Python$94:我目前被困在这个问题上。我能找到的最好的例子就是这个。
def phonebook():
return """
Mary:893-0234:Realtor:
Fred:897-2033:Boulder crusher:
Barney:234-2342:Professional bowler:"""
def phones():
phones = phonebook()
phonelist = phones.split('\n')
newphonelist = []
for list in phonelist:
newphonelist = newphonelist + [list.split(":")]
return newphonelist
def findPhone(person):
for people in phones():
if people[0] == person:
print "Phone number for",person,"is",people[1]
如你看到的。问题是它只返回电话号码和他们的头衔。我需要做的是只返回一个类的名称和等级以及只有 2 个输入(名称、主题)的类。这是我到目前为止所拥有的。
def scoreList():
return"""
Justin:Calculus$90:Java$85:Python$88:
Taylor:Calculus$73:Java$95:Python$86:
Drew:Calculus$80:Java$75:Python$94:"""
def scores():
scores=scoreList()
scorelist=scores.split('\n')
newscorelist=[]
for list in scorelist:
newscorelist=newscorelist + [list.split(":")]
scores.split('$')
return newscorelist
def findScore(student,subject):
for people in scores():
for subject in scores():
if people[0]==student:
if subject[0]==subject:
print (student,"got",score,"of the course",subject[1])
是的,我是这方面的新手。不过,我这几天一直在寻找如何做到这一点。