如何修复程序中的<zip object at 0x03B1C3C8>
class Student(object):
def __init__(self, name, surname, grade, hours):
self.name = name
self.surname = surname
self.grade = grade
self.hours = hours
def getName(self):
return '{}'.format(self.name)
def getSurname(self):
return '{}'.format(self.surname)
def getGrade(self):
return zip(self.grade, self.hours)
def getHours(self):
return '{}'.format(self.hours)
def getQPoints(self):
pass
stud1 = Student("John","Brown",["A","B","A"],["15.0","25.0","20.0"])
stud2 = Student("Mary","Watson","B","20")
print (stud1.getGrade())
print (stud1.getHours())
答案 0 :(得分:0)
只需使用list()
,如下所示:
def getGrade(self):
return list(zip(self.grade, self.hours))
注意:如果您只需要循环返回的list()
对象,则无需应用zip
。
>>> for i, j in stud1.getGrade():
... print(i, j)
...
('A', '15.0')
('B', '25.0')
('A', '20.0')
答案 1 :(得分:0)
我说你完全忘了拉链:
class Student(object):
def __init__(self, name, surname, grade, hours):
self.name = name
self.surname = surname
self.grade = grade
self.hours = hours
def getName(self):
return '{}'.format(self.name)
def getSurname(self):
return '{}'.format(self.surname)
def getGrade(self):
return (self.grade, self.hours)
def getHours(self):
return '{}'.format(self.hours)
def getQPoints(self):
pass
stud1 = Student("John","Brown",["A","B","A"],["15.0","25.0","20.0"])
stud2 = Student("Mary","Watson","B","20")
print (stud1.getGrade())
print (stud1.getHours())
return (self.grade, self.hours)
将创建一个可以打印的元组
答案 2 :(得分:0)
从python文档中,当你使用zip
时,它返回一个元组的迭代器(python使用这些&#39; ()
&#39;来定义。)
如此迭代你的成绩元组可能是最简单的:
for grade in stud1.getGrades():
print("Grade: " + grade[0] + " Hours studied: " + grade[1])
这会给你:
Grade: A Hours studied: 15.0
Grade: B Hours studied: 25.0
Grade: A Hours studied: 20.0
但我宁愿让Student.getGrade
仅返回成绩列表。然后有另一个函数getReport
来打印小时数和成绩。