1
ZeldaPeach 2019-08-03 14:44:37 +08:00
{k: data[k] for k in ('a','b')}
|
2
Trim21 2019-08-03 14:44:41 +08:00 via Android
用字典生成式
|
3
Sylv 2019-08-03 14:45:31 +08:00
new_keys = ['a', 'b']
new_data = {key: data[key] for key in new_keys} |
4
shanguo 2019-08-04 00:47:06 +08:00
>>> def select(src_dict, keys):
... return {k: src_dict[k] for k in keys if src_dict.get(k)} ... >>> d = dict(name='jok', age=20) >>> select(d, ['name']) {'name': 'jok'} |