是否可以以比下面更简洁的方式为字典的多个键指定值?
我的意思是,让d
成为一个字典,如下所示:
d={'a':1,'b':2,'c':3}
要为多个键指定值,我需要这样做:
d['a']=10
d['b']=200
d['c']=30
我可以通过以下方式实现相同目标:
d['a','b','c']=10,200,30
感谢。
答案 0 :(得分:45)
答案 1 :(得分:4)
您始终可以将其包装在一个函数中:
def multiassign(d, keys, values):
d.update(zip(keys, values))
即使您不了解update
,也可以这样写:
def multiassign(d, keys, values):
for k, v in zip(keys, values):
d[k] = v
或者您甚至可以编写一个dict
子类,它可以为您提供所需的语法:
class EasyDict(dict):
def __getitem__(self, key):
if isinstance(key, tuple):
return [super().__getitem__(k) for k in key]
else:
return super().__getitem__(key)
def __setitem__(self, key, value):
if isinstance(key, tuple):
self.update(zip(key, value))
else:
super().__setitem__(key, value)
def __delitem__(self, key, value):
if isinstance(key, tuple):
for k in key: super().__delitem__(k)
else:
super().__setitem__(key, value)
现在:
>>> d = {'a': 1, 'd': 4}
>>> multiassign(d, ['a', 'b', 'c'], [10, 200, 300])
>>> d
{'a': 10, 'b': 200, 'c': 300, 'd': 4}
>>> d2 = EasyDict({'a': 1, 'd': 4})
>>> d2['a', 'b', 'c'] = 100, 200, 300
>>> d2
{'a': 10, 'b': 200, 'c': 300, 'd': 4}
请注意,显然不再可能将元组用作EasyDict
中的键。
此外,如果您打算将此用于严重的事情,您可能希望改进错误处理。 (d['a', 'b'] = 1
会发出关于zip argument #2 must support iteration
的隐秘信息,d['a', 'b', 'c'] = 1, 2
会默默工作,不会对c
等做任何事情。)
答案 2 :(得分:3)
您也可以使用multiple assigment semantics:
d['a'], d['b'], d['c'] = 10, 200, 30
答案 3 :(得分:3)
速度比较,从最差到最好:
Python 3.5.3 |Continuum Analytics, Inc.| (default, May 15 2017, 10:43:23) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import numpy.random as nprnd
...: d = dict([(_, nprnd.rand()) for _ in range(1000)])
...: values = nprnd.randint(1000, size=10000)
...: keys = nprnd.randint(1000, size=10000)
...: def multiassign(d, keys, values):
...: for k, v in zip(keys, values):
...: d[k] = v
...:
...: d1 = dict(d)
...: %timeit multiassign(d1, keys, values)
...: d1 = dict(d)
...: %timeit {**d1, **{keys[i]: values[i] for i in range(len(keys))}}
...: d1 = dict(d)
...: %timeit d1.update(zip(keys, values))
...: d1 = dict(d)
...: %timeit {*d1.items(), *zip(keys, values)}
...: d1 = dict(d)
...: %timeit {**d1, **{key: value for key, value in zip(keys, values)}}
...: d1 = dict(d)
...: %timeit {**d1, **dict(zip(keys, values))}
4 ms ± 25.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
3.66 ms ± 29.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
3.17 ms ± 31.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
2.81 ms ± 98.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
2.38 ms ± 75.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
1.96 ms ± 21 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
所以明显的赢家是从字典中重新创建词典。
答案 4 :(得分:1)
你可以使用字典理解,例如
letters, nos = ['a','b','c'], [1,2,3]
d = {letters[i]: nos[i] for i in range(len(nos))}
输出:
{'a':1,'c':3,'b':2}