简体   繁体   English

无法在Python中更新字典

[英]Can't update a dictionary in Python

I'm trying to add some records into a dictionary. 我正在尝试将一些记录添加到字典中。

Initially I was doing it this way 最初我是这样做的

licenses = [dict(licenseid=row[0], client=row[1], macaddress=row[2], void=row[18]) for row in db]

But I've since realized I need to do some processing to filter records from db, so I tried changing the code to: 但我已经意识到我需要做一些处理来从db中过滤记录,所以我尝试将代码更改为:

for rec in db:
    if rec['deleted'] == False:
       licenses.update(dict(licenseid=row[0], client=row[1], macaddress=row[2], void=row[18])

That code runs without exceptions, but I only end up with the last db record in licenses, which is confusing me. 该代码运行没有例外,但我最终只得到许可证中的最后一个db记录,这让我感到困惑。

I think licenses is a list: 我认为licenses是一个列表:

licenses = []
...

and you should append to it new dictionaries: 你应该附加新的词典:

licenses.append(dict(...))

If I understand correctly, you want to add multiple records in a single dictionary, right ? 如果我理解正确,你想在一个字典中添加多个记录,对吧? Instead of making a list of dictionaries, why wouldn't you make a dictionary of lists instead? 而不是制作字典列表,为什么不制作列表字典呢?

  • Start by building a list of the keys you'll need (so that you always access them in the same order). 首先构建一个您需要的密钥列表(这样您始终可以按相同的顺序访问它们)。

     keys = ["licenses", "client", "macaddress", "void"] 
  • Construct an empty dictionary: 构造一个空字典:

     licences = dict((k,[]) for k in keys] 
  • Recursively add entries to your dictionary: 以递归方式向词典中添加条目:

     for (k,item) in row: dict[k].append(item) 

Of course, it might be easier to build a list of all your records first, and then construct a dictionary at the very end. 当然,首先构建所有记录的列表可能更容易,然后在最后构建一个字典。

Quoth the dict.update() documentation: dict.update()文档:

update([other]) Update the dictionary with the key/value pairs from other, overwriting existing keys. 更新([other])使用其他键中的键/值对更新字典,覆盖现有键。 Return None. 返回无。

Which explains why the last update "wins". 这解释了为什么上次更新“获胜”。 licences cannot be a list as there is no update method for lists. licences不能是列表,因为列表没有更新方法。

If the code in your post is your genuine code, then you might consider replacing row with rec in the last line (the one with the update), because there are chances you're updating your dictionary with always the same values ! 如果帖子中的代码是您的正版代码,那么您可以考虑在最后一行(带有更新的行)中用rec替换行,因为您可能会使用相同的值更新字典!

Edit: There's obviously something very wrong in this code, from the other answer I see that I overlooked the fact that licenses was declared as a list: so the only explanation for not having an exception is either the snippets you show are not the genuine one or all your record are so that rec['deleted'] is True (so that the update method is never called). 编辑:在这段代码中显然有一些非常错误,从另一个答案我看到我忽略了许可证被声明为列表的事实:因此,没有例外的唯一解释是你显示的片段不是真正的片段或者你的所有记录都是rec ['deleted']为True(因此永远不会调用更新方法)。

After responses, I've amended my code: 回复后,我修改了我的代码:

licenses = []

for row in db:
        if row.deleted == False:
            licenses.append(dict(licenseid=row[0], client=row[1], macaddress=row[2], void=row[18]))

Which now works perfectly. 现在效果很好。 Thanks for spotting my stupidity! 感谢你发现我的愚蠢! ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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