Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python默认字典vs.get()_Python_Dictionary - Fatal编程技术网

使用python默认字典vs.get()

使用python默认字典vs.get(),python,dictionary,Python,Dictionary,我想要一本默认为“无”的词典。我有两种方法来实施: 1) 使用collections.defaultdict并将默认值设置为None。 2) 使用普通字典,但使用.get()获取值。当该键不存在时,默认情况下返回None 哪种方法更好?如果您只希望默认值为无。它不会比使用.get()方法更简单 或者,如果希望随后将该值设置为“无”,则可以使用该方法 要使用defaultdict,您可以编写如下内容 from collections import defaultdict b = defaultd

我想要一本默认为“无”的词典。我有两种方法来实施: 1) 使用collections.defaultdict并将默认值设置为None。

2) 使用普通字典,但使用.get()获取值。当该键不存在时,默认情况下返回None


哪种方法更好?

如果您只希望默认值为无。它不会比使用.get()方法更简单

或者,如果希望随后将该值设置为“无”,则可以使用该方法

要使用defaultdict,您可以编写如下内容

from collections import defaultdict
b = defaultdict(lambda: None) # this lambda returns the default value, 
# None in this case

b[1] = "1"
print(b[1]) # prints 1
print(b[2]) # prints None

正如青岛军佐在他的回答中指出的那样,.get()比使用默认字典更快。所以最好还是坚持下去。但是,如果您需要返回非None的值,如果您只希望默认值为None,那么defaultdict就是答案。它不会比使用.get()方法更简单

或者,如果希望随后将该值设置为“无”,则可以使用该方法

要使用defaultdict,您可以编写如下内容

from collections import defaultdict
b = defaultdict(lambda: None) # this lambda returns the default value, 
# None in this case

b[1] = "1"
print(b[1]) # prints 1
print(b[2]) # prints None

正如青岛军佐在他的回答中指出的那样,.get()比使用默认字典更快。所以最好还是坚持下去。但是,如果您需要返回除None之外的其他内容,则defaultdict就是答案

defaultdict将使用比dict.get()更多的内存,因为如果找不到键,defaultdict将使用该键在包装的dict中设置默认值

另一方面,如果在dict.get()中找不到键,则直接返回默认值。不需要额外的空间

使用python2使用空dict运行2亿次

200000000  199.584    0.000  283.804    0.000 default.py:11(get_default)
200000000  158.268    0.000  158.268    0.000 default.py:14(get_defaultdict)
Line #    Mem usage    Increment   Line Contents
================================================
17   10.918 MiB   10.918 MiB   @profile
18                             def run_get_default():
19   10.918 MiB    0.000 MiB       h = {}
20   10.918 MiB    0.000 MiB       for i in xrange(0, COUNT):
21   10.918 MiB    0.000 MiB           get_default(h, i)
22   10.918 MiB    0.000 MiB           get_default(h, i)

Line #    Mem usage    Increment   Line Contents
================================================
24   10.918 MiB   10.918 MiB   @profile
25                             def run_get_defaultdict():
26   10.918 MiB    0.000 MiB       h = defaultdict(int)
27   83.496 MiB    7.273 MiB       for i in xrange(0, COUNT):
28   83.496 MiB   58.141 MiB           get_defaultdict(h, i)
29   83.496 MiB    7.164 MiB           get_defaultdict(h, i)
使用python2以空dict运行100万个数据时的内存使用情况

200000000  199.584    0.000  283.804    0.000 default.py:11(get_default)
200000000  158.268    0.000  158.268    0.000 default.py:14(get_defaultdict)
Line #    Mem usage    Increment   Line Contents
================================================
17   10.918 MiB   10.918 MiB   @profile
18                             def run_get_default():
19   10.918 MiB    0.000 MiB       h = {}
20   10.918 MiB    0.000 MiB       for i in xrange(0, COUNT):
21   10.918 MiB    0.000 MiB           get_default(h, i)
22   10.918 MiB    0.000 MiB           get_default(h, i)

Line #    Mem usage    Increment   Line Contents
================================================
24   10.918 MiB   10.918 MiB   @profile
25                             def run_get_defaultdict():
26   10.918 MiB    0.000 MiB       h = defaultdict(int)
27   83.496 MiB    7.273 MiB       for i in xrange(0, COUNT):
28   83.496 MiB   58.141 MiB           get_defaultdict(h, i)
29   83.496 MiB    7.164 MiB           get_defaultdict(h, i)
defaultdict需要一个可调用的值作为dict默认值,您还可以使用函数或lambda根据需要自定义自己的默认值

dict.get($key,$default)更容易获得每个记录的默认值


谢谢

defaultdict将使用比dict.get()更多的内存,因为如果找不到键,defaultdict将使用该键在包装的dict中设置默认值

另一方面,如果在dict.get()中找不到键,则直接返回默认值。不需要额外的空间

使用python2使用空dict运行2亿次

200000000  199.584    0.000  283.804    0.000 default.py:11(get_default)
200000000  158.268    0.000  158.268    0.000 default.py:14(get_defaultdict)
Line #    Mem usage    Increment   Line Contents
================================================
17   10.918 MiB   10.918 MiB   @profile
18                             def run_get_default():
19   10.918 MiB    0.000 MiB       h = {}
20   10.918 MiB    0.000 MiB       for i in xrange(0, COUNT):
21   10.918 MiB    0.000 MiB           get_default(h, i)
22   10.918 MiB    0.000 MiB           get_default(h, i)

Line #    Mem usage    Increment   Line Contents
================================================
24   10.918 MiB   10.918 MiB   @profile
25                             def run_get_defaultdict():
26   10.918 MiB    0.000 MiB       h = defaultdict(int)
27   83.496 MiB    7.273 MiB       for i in xrange(0, COUNT):
28   83.496 MiB   58.141 MiB           get_defaultdict(h, i)
29   83.496 MiB    7.164 MiB           get_defaultdict(h, i)
使用python2以空dict运行100万个数据时的内存使用情况

200000000  199.584    0.000  283.804    0.000 default.py:11(get_default)
200000000  158.268    0.000  158.268    0.000 default.py:14(get_defaultdict)
Line #    Mem usage    Increment   Line Contents
================================================
17   10.918 MiB   10.918 MiB   @profile
18                             def run_get_default():
19   10.918 MiB    0.000 MiB       h = {}
20   10.918 MiB    0.000 MiB       for i in xrange(0, COUNT):
21   10.918 MiB    0.000 MiB           get_default(h, i)
22   10.918 MiB    0.000 MiB           get_default(h, i)

Line #    Mem usage    Increment   Line Contents
================================================
24   10.918 MiB   10.918 MiB   @profile
25                             def run_get_defaultdict():
26   10.918 MiB    0.000 MiB       h = defaultdict(int)
27   83.496 MiB    7.273 MiB       for i in xrange(0, COUNT):
28   83.496 MiB   58.141 MiB           get_defaultdict(h, i)
29   83.496 MiB    7.164 MiB           get_defaultdict(h, i)
defaultdict需要一个可调用的值作为dict默认值,您还可以使用函数或lambda根据需要自定义自己的默认值

dict.get($key,$default)更容易获得每个记录的默认值


谢谢

试试这个帖子:和往常一样,“哪个更好?”的答案是“那要看情况而定”。这些方法都有权衡。你需要它做什么?试试这个帖子:和往常一样,“哪个更好?”的答案是“那要看情况而定”。这些方法都有权衡。你需要它做什么?这不是地址
defaultdict
whoops,我有点忘了,谢谢@juanpa.arrivillaga这不是地址
defaultdict
whoops,我有点忘了,谢谢@juanpa.arrivillaga