Атрибут Python-Print Specific Object List - PullRequest
0 голосов
/ 26 ноября 2018

Как напечатать определенный атрибут объекта из списка объектов.Я думал, что смогу предоставить конкретный индекс списка, но я получаю следующую ошибку при попытке: 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)

1 Ответ

0 голосов
/ 26 ноября 2018

У вас есть некоторые ошибки здесь.

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...