[英]Splitting a nested dictionary based on value of nested key
I have a nested dictionary that I would like to split based on the value of end qty
. 我有一个嵌套字典,我想根据end qty
的值进行拆分。 I would like to only keep keys in which 'end qty' == 0
. 我只想保留'end qty' == 0
。 I believe it can be done easily with dictionary comprehension, but I can't quite get it right. 我相信通过字典理解可以轻松完成此操作,但我不太正确。
import datetime
d = {
'ID1' : {'start qty': 13 , 'end qty': 40},
'ID2' : {'start qty': 10 , 'end qty': 0},
'UD3' : {'start qty': 30 , 'end qty': 30},
'ID4' : {'start qty': 20 , 'end qty': 0},
}
print { k:v for k, v in d.items() if ['end qty'] == 0 }
尝试以下这一行:
print { k:v for k, v in d.items() if v['end qty'] == 0 }
只需以这种方式在print语句中添加v
print { k:v for k, v in d.items() if v['end qty'] == 0 }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.