List 属性错误:';元组';对象没有属性';项目'; AttributeError回溯(最近一次调用) 在() 35 # 36打开('res','w')作为f: --->37 s=已排序(s.items(),key=lambda x:x[1],reverse=True) 38对于s中的i: 39 f.writelines(str(i[0])+“:“+str(i[1])+”\n”) AttributeError:“列表”对象没有属性“项”

List 属性错误:';元组';对象没有属性';项目'; AttributeError回溯(最近一次调用) 在() 35 # 36打开('res','w')作为f: --->37 s=已排序(s.items(),key=lambda x:x[1],reverse=True) 38对于s中的i: 39 f.writelines(str(i[0])+“:“+str(i[1])+”\n”) AttributeError:“列表”对象没有属性“项”,list,python-3.x,List,Python 3.x,对于上面的代码,我一直遇到这个错误,似乎无法修复它。如何防止引发属性错误?项适用于列表中不能使用的词典 AttributeError Traceback (most recent call last) <ipython-input-24-3e6cc449e797> in <module>() 35 # 36 with open('res','w') as f: ---> 37 s=s

对于上面的代码,我一直遇到这个错误,似乎无法修复它。如何防止引发
属性错误

项适用于列表中不能使用的词典

AttributeError                            Traceback (most recent call last)
<ipython-input-24-3e6cc449e797> in <module>()
     35 # 
     36 with open('res','w') as f:
---> 37     s=sorted(s.items(),key=lambda x: x[1],reverse=True)
     38     for i in s:
     39         f.writelines(str(i[0]) + " : " + str(i[1])+"\n")

AttributeError: 'list' object has no attribute 'items'
>d={1:'a'}
>>>d.项目()
口述项目([(1,'a'))
>>>l=[1,2]
>>>l.项目()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“列表”对象没有属性“项”

项目是为您不能在列表中使用的词典而设

AttributeError                            Traceback (most recent call last)
<ipython-input-24-3e6cc449e797> in <module>()
     35 # 
     36 with open('res','w') as f:
---> 37     s=sorted(s.items(),key=lambda x: x[1],reverse=True)
     38     for i in s:
     39         f.writelines(str(i[0]) + " : " + str(i[1])+"\n")

AttributeError: 'list' object has no attribute 'items'
>d={1:'a'}
>>>d.项目()
口述项目([(1,'a'))
>>>l=[1,2]
>>>l.项目()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“列表”对象没有属性“项”

s的内容是什么?我假设s是一个列表,而不是数组/元组,正如错误所示。s是dict……您发布的代码包含一个IndentationError。因为您没有显示
s
真正是什么,所以错误是不可验证的。请输入问题,以便可以复制和粘贴(以便我们也可以看到异常!)。它应该只包含产生错误所需的必要代码(因此可以完全删除文件打开)。请查看并在编辑问题时确保提供一个。很抱歉,我犯了一个错误,因为这是第一次在stackoverflow中提问。正如你所说,这是一个缩进问题。感谢您的帮助。您是否可能正在更改某个线程中其他位置的
s
值?但是无论如何,您应该调试
s
第36行,以确保它是一个
dict
。s的内容是什么?我假设s是一个列表,而不是数组/元组,正如错误所示。s是dict……您发布的代码包含一个IndentationError。因为您没有显示
s
真正是什么,所以错误是不可验证的。请输入问题,以便可以复制和粘贴(以便我们也可以看到异常!)。它应该只包含产生错误所需的必要代码(因此可以完全删除文件打开)。请查看并在编辑问题时确保提供一个。很抱歉,我犯了一个错误,因为这是第一次在stackoverflow中提问。正如你所说,这是一个缩进问题。感谢您的帮助。您是否可能正在更改某个线程中其他位置的
s
值?但是无论如何,您应该调试
s
第36行,以确保它是
dict
>>> d = {1:'a'}
>>> d.items()
dict_items([(1, 'a')])

>>> l = [1,2]
>>> l.items()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'items'