[英]Python- Print Specific Object List Attribute
如何从对象列表中打印特定的对象属性。 我以为可以提供特定的列表索引,但尝试尝试时会出现以下错误: AttributeError: 'list' object has no attribute 'grade'
class tests():
def __init__(self,grade):
self.grade = grade
test_list = []
for x in range(1,6):
test_object = tests(x)
test_list.append(test_object)
print (test_list[1].grade)
您这里有一些错误。
class tests():
def __init__(self,grade):
self.grade = grade
test_list = []
for x in range(1,6):
test_object = tests(x)
test_list.append(test_object)
list_a = [5,3,2,1,4]
# THIS LINE BELOW IS THE PROBLEM
for x in [test_list]:
# in this line below X = test_list and indeed test_list.grade doesn't make sense.
test_list.append(sorted(x.grade,key=list_a.index))
print (test_list[1].grade)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.