0

我将如何使用字符串和拆分来从列表中三个特定人的三个不同等级中获得某个等级。这是清单。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])

是的,我是这方面的新手。不过,我这几天一直在寻找如何做到这一点。

4

1 回答 1

0

我建议使用字典词典。

首先创建你的字典

students = {}

现在为每一行创建一个学生

for studentdata in scores.split('\n'):
    data = studentdata.split(':')
    name = data[0]
    students[name] = {}

现在将每个班级添加为学生字典中的键

for class_data in data[1:]:
    if class_data:
        class_name,class_score = class_data.split('$')
        students[name][class_name] = class_score

把它们放在一起你会得到:

students = {}
for studentdata in scores.split('\n'):
    data = studentdata.split(':')
    name = data[0]
    students[name] = {}
    for class_data in data[1:]:
        if class_data:
            class_name,class_score = class_data.split('$')
            students[name][class_name] = class_score

学生现在是字典的字典。现在要查找与学生相关的分数findScore是:

def findScore(student,subject):
    print "student %s got %s of the course %s" % (student,students[student][subject],subject)

不能保证学生和学科都在字典中,如果它们不是就会发生错误。为了防止这种情况,只需检查该键是否在字典中:

def findScore(student,subject):
    if student in students:
        if subject in students[student]:
            print "student %s got %s of the course %s" % (student,students[student][subject],subject)
        else:
            print "subject %s not found for student %s" % (subject,student)
    else:
        print "student %s not found" % (student)

现在真的把它们放在一起你会得到:

scores = """Justin:Calculus$90:Java$85:Python$88:
Taylor:Calculus$73:Java$95:Python$86:
Drew:Calculus$80:Java$75:Python$94:"""

students = {}
for studentdata in scores.split('\n'):
    data = studentdata.split(':')
    name = data[0]
    students[name] = {}
    for class_data in data[1:]:
        if class_data:
            class_name,class_score = class_data.split('$')
            students[name][class_name] = class_score

def findScore(student,subject):
    if student in students:
        if subject in students[student]:
            print "student %s got %s of the course %s" % (student,students[student][subject],subject)
        else:
            print "subject %s not found for student %s" % (subject,student)
    else:
        print "student %s not found" % (student)

#do some testing
findScore("Bob","Painting")
findScore("Taylor","Painting")
findScore("Taylor","Calculus")

可以在此处找到有关字典(以及其他数据结构)的更多信息

于 2013-07-23T16:25:04.693 回答