[英]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.