社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

S.McWhorter • 7 年前 • 1924 次点击  

如何从对象列表中打印特定的对象属性。我想我可以提供特定的列表索引,但是当我尝试时,出现了以下错误: 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)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43103
文章 [ 1 ]  |  最新文章 7 年前
Nick Chapman
Reply   •   1 楼
Nick Chapman    8 年前

你这儿有虫子。

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)