Jython -Python如何返回字符串的不同部分以获得成绩

时间:2013-07-23 15:47:26

标签: python string split jython

我将如何使用字符串和拆分,以便能够从列表中的三个特定人中获得三个不同等级的特定等级。这是清单。  贾斯汀:微积分$ 90:Java的$ 85:Python的$ 88:  泰勒:积分$ 73:Java的$ 95:Python的$ 86:  德鲁:微积分$ 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])

是的,我是新手。我现在一直在寻找如何做这几天。

1 个答案:

答案 0 :(得分: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")

可以找到关于词典(以及其他数据结构)的更多信息here