python 合并字典效率问题
2015-03-25 15:04
239 查看
原文地址: http://www.dewen.io/q/2002
x
=
{'a':1,
'b':
2}
;
y
=
{'b':10,
'c':
11}
//方法一
z
= dict(x.items()+y.items());
//方法二
z
= dict(z,**y)
//方法三
z
= x.copy()
z.update(y)
//方法四
z
=
(lambda
a, b:
(lambda
a_copy: a_copy.update(b)
or a_copy)(a.copy()))(x,
y)
In [17]: timeit.timeit("dict(x,**y)", "x
= {'a':1, 'b': 2};y={'b':10, 'c':11}")
Out[17]: 0.6188690662384033
In [18]: timeit.timeit("dict(x.items()+y.items())", "x
= {'a':1, 'b': 2};y={'b':10, 'c':11}")
Out[18]: 1.8446910381317139
In [19]: timeit.timeit("dict(x,**y)", "x
= {'a':1, 'b': 2};y={'b':10, 'c':11}")
Out[19]: 0.6554520130157471
In [20]: timeit.timeit("z=x.copy();z.update(y)", "x
= {'a':1, 'b': 2};y={'b':10, 'c':11}")
Out[20]: 0.5006368160247803
In [21]: timeit.timeit("(lambda
a, b: (lambda a_copy: a_copy.update(b) or a_copy)(a.copy()))(x, y)", "x = {'a':1, 'b': 2};y={'b':10, 'c':11}")
Out[21]: 1.0203309059143066
x
=
{'a':1,
'b':
2}
;
y
=
{'b':10,
'c':
11}
//方法一
z
= dict(x.items()+y.items());
//方法二
z
= dict(z,**y)
//方法三
z
= x.copy()
z.update(y)
//方法四
z
=
(lambda
a, b:
(lambda
a_copy: a_copy.update(b)
or a_copy)(a.copy()))(x,
y)
In [17]: timeit.timeit("dict(x,**y)", "x
= {'a':1, 'b': 2};y={'b':10, 'c':11}")
Out[17]: 0.6188690662384033
In [18]: timeit.timeit("dict(x.items()+y.items())", "x
= {'a':1, 'b': 2};y={'b':10, 'c':11}")
Out[18]: 1.8446910381317139
In [19]: timeit.timeit("dict(x,**y)", "x
= {'a':1, 'b': 2};y={'b':10, 'c':11}")
Out[19]: 0.6554520130157471
In [20]: timeit.timeit("z=x.copy();z.update(y)", "x
= {'a':1, 'b': 2};y={'b':10, 'c':11}")
Out[20]: 0.5006368160247803
In [21]: timeit.timeit("(lambda
a, b: (lambda a_copy: a_copy.update(b) or a_copy)(a.copy()))(x, y)", "x = {'a':1, 'b': 2};y={'b':10, 'c':11}")
Out[21]: 1.0203309059143066
相关文章推荐
- Python 合并两个字典的各种方法以及效率的比较
- Python合并两个字典的常用方法与效率比较
- Python 合并两个字典的各种方法以及效率的比较
- python字典合并哪种方式效率最高?
- Python字典key值查询效率低的问题
- Python合并两个字典的常用方法与效率比较
- 【转载】Python中如何高效实现两个字典合并,三种方法比较。
- Python几种语句执行效率问题
- python 字典对象赋值之deepcopy遭遇的问题及解决过程(lxml惹的祸)
- Python中实现两个字典(dict)合并的方法
- python字典value为列表时遇到的问题
- Python 中两个字典(dict)合并
- python中的效率问题
- Python 中两个字典(dict)合并
- Python 字典中的中文输出问题
- Python numpy数组扩展效率问题
- “带锁的门”问题,并有c语言和python代码运行效率对比
- python 字典对象赋值之deepcopy遭遇的问题及解决过程(lxml惹的祸)
- python解决字典中的值是列表问题的方法
- python字典(dictionary)使用:基本函数code实例,字典的合并、排序、copy,函数中*args 和**kwargs做形参和实参