简体   繁体   English

Python-打印特定对象列表属性

[英]Python- Print Specific Object List Attribute

How do I print a specific object attribute from a list of objects. 如何从对象列表中打印特定的对象属性。 I was thinking I could provide the specific list index but I get the following error when I try that: AttributeError: 'list' object has no attribute 'grade' 我以为可以提供特定的列表索引,但尝试尝试时会出现以下错误: 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)

You have some bugs here. 您这里有一些错误。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM