附加到 Python 字典中的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26367812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Appending to list in Python dictionary
提问by Michael Murphy
Is there a more elegant way to write this code?
有没有更优雅的方式来编写这段代码?
What I am doing: I have keys and dates. There can be a number of dates assigned to a key and so I am creating a dictionary of lists of dates to represent this. The following code works fine, but I was hoping for a more elegant and Pythonic method.
我在做什么:我有钥匙和日期。可以有多个日期分配给一个键,因此我正在创建一个日期列表字典来表示这一点。下面的代码工作正常,但我希望有一个更优雅和 Pythonic 的方法。
dates_dict = dict()
for key, date in cur:
if key in dates_dict:
dates_dict[key].append(date)
else:
dates_dict[key] = [date]
I was expecting the below to work, but I keep getting a NoneType has no attribute append error.
我期待下面的工作,但我不断收到 NoneType has no attribute append 错误。
dates_dict = dict()
for key, date in cur:
dates_dict[key] = dates_dict.get(key, []).append(date)
This probably has something to do with the fact that
这可能与以下事实有关
print([].append(1))
None
but why?
但为什么?
采纳答案by thefourtheye
list.appendreturns None, since it is an in-place operation and you are assigning it back to dates_dict[key]. So, the next time when you do dates_dict.get(key, []).appendyou are actually doing None.append. That is why it is failing. Instead, you can simply do
list.append返回None,因为它是一个就地操作并且您将其分配回dates_dict[key]. 所以,下次你做的时候,你dates_dict.get(key, []).append实际上是在做None.append. 这就是它失败的原因。相反,你可以简单地做
dates_dict.setdefault(key, []).append(date)
But, we have collections.defaultdictfor this purpose only. You can do something like this
但是,我们只是collections.defaultdict为了这个目的。你可以做这样的事情
from collections import defaultdict
dates_dict = defaultdict(list)
for key, date in cur:
dates_dict[key].append(date)
This will create a new list object, if the keyis not found in the dictionary.
如果key在字典中找不到,这将创建一个新的列表对象。
Note:Since the defaultdictwill create a new list if the key is not found in the dictionary, this will have unintented side-effects. For example, if you simply want to retrieve a value for the key, which is not there, it will create a new list and return it.
注意:由于defaultdict如果在字典中找不到键,将创建一个新列表,这将产生意想不到的副作用。例如,如果您只想检索键的值,而该值不存在,它将创建一个新列表并返回它。
回答by Padraic Cunningham
dates_dict[key] = dates_dict.get(key, []).append(date)sets dates_dict[key]to Noneas list.appendreturns None.
dates_dict[key] = dates_dict.get(key, []).append(date)设置 dates_dict[key]于None作为list.append回报None。
In [5]: l = [1,2,3]
In [6]: var = l.append(3)
In [7]: print var
None
You should use collections.defaultdict
import collections
dates_dict = collections.defaultdict(list)
回答by jfs
Is there a more elegant way to write this code?
有没有更优雅的方式来编写这段代码?
from collections import defaultdict
dates_dict = defaultdict(list)
for key, date in cur:
dates_dict[key].append(date)

