python字典错误AttributeError: 'list'对象没有属性'key '

时间:2022-02-05 18:18:12

I have an error with this line. I am working with a dictionary from a file with an import. This is the dictionary:

这条线有个误差。我正在使用一个具有导入的文件中的字典。这是字典:

users = [{'id':1010,'name':"Administrator",'type':1},{'id':1011,'name':"Administrator2",'type':1}]

And the method with which the work is as follows:

工作方法如下:

def addData(dict, entry):
new = {}
x = 0
for i in dict.keys():
    new[i] = entry(x)
    x += 1
dict.append(new)

Where "dict" would be "users", but the error is that the dictionary does not recognize me as such. Can anyone tell me, I have wrong in the dictionary?

“dict”应该是“用户”,但错误的是字典不承认我本身。谁能告诉我,我的字典错了吗?

2 个解决方案

#1


6  

Perhaps you are looking to do something along these lines:

也许你想做一些类似的事情:

users = [{'id':1010,'name':"Administrator",'type':1},{'id':1011,'name':"Administrator2",'type':1}]

new_dict={}

for di in users:
    new_dict[di['id']]={}
    for k in di.keys():
        if k =='id': continue
        new_dict[di['id']][k]=di[k]

print new_dict     
# {1010: {'type': 1, 'name': 'Administrator'}, 1011: {'type': 1, 'name': 'Administrator2'}} 

Then you can do:

然后你可以做:

>>> new_dict[1010] 
{'type': 1, 'name': 'Administrator'}

Essentially, this is turning a list of anonymous dicts into a dict of dicts that are keys from the key 'id'

本质上,这是将一个匿名的命令列表转换为一个来自键“id”的键的命令

#2


6  

That's not a dicionary, it's a list of dictionaries!
EDIT: And to make this a little more answer-ish:

那不是一本词典,而是一本字典!编辑:让这个更有答案:

users = [{'id':1010,'name':"Administrator",'type':1},{'id':1011,'name':"Administrator2",'type':1}]
newusers = dict()
for ud in users:
    newusers[ud.pop('id')] = ud
print newusers
#{1010: {'type': 1, 'name': 'Administrator'}, 1011: {'type': 1, 'name': 'Administrator2'}}
newusers[1012] = {'name': 'John', 'type': 2}
print newusers
#{1010: {'type': 1, 'name': 'Administrator'}, 1011: {'type': 1, 'name': 'Administrator2'}, 1012: {'type': 2, 'name': 'John'}}

Which is essentially the same as dawgs answer, but with a simplified approach on generating the new dictionary

这和dawgs的回答本质上是一样的,但是用一种简化的方法生成新的字典

#1


6  

Perhaps you are looking to do something along these lines:

也许你想做一些类似的事情:

users = [{'id':1010,'name':"Administrator",'type':1},{'id':1011,'name':"Administrator2",'type':1}]

new_dict={}

for di in users:
    new_dict[di['id']]={}
    for k in di.keys():
        if k =='id': continue
        new_dict[di['id']][k]=di[k]

print new_dict     
# {1010: {'type': 1, 'name': 'Administrator'}, 1011: {'type': 1, 'name': 'Administrator2'}} 

Then you can do:

然后你可以做:

>>> new_dict[1010] 
{'type': 1, 'name': 'Administrator'}

Essentially, this is turning a list of anonymous dicts into a dict of dicts that are keys from the key 'id'

本质上,这是将一个匿名的命令列表转换为一个来自键“id”的键的命令

#2


6  

That's not a dicionary, it's a list of dictionaries!
EDIT: And to make this a little more answer-ish:

那不是一本词典,而是一本字典!编辑:让这个更有答案:

users = [{'id':1010,'name':"Administrator",'type':1},{'id':1011,'name':"Administrator2",'type':1}]
newusers = dict()
for ud in users:
    newusers[ud.pop('id')] = ud
print newusers
#{1010: {'type': 1, 'name': 'Administrator'}, 1011: {'type': 1, 'name': 'Administrator2'}}
newusers[1012] = {'name': 'John', 'type': 2}
print newusers
#{1010: {'type': 1, 'name': 'Administrator'}, 1011: {'type': 1, 'name': 'Administrator2'}, 1012: {'type': 2, 'name': 'John'}}

Which is essentially the same as dawgs answer, but with a simplified approach on generating the new dictionary

这和dawgs的回答本质上是一样的,但是用一种简化的方法生成新的字典