如何编写这个以最佳方式从字典中检索值的简单代码(pythonic)
我有以下代码如何编写这个以最佳方式从字典中检索值的简单代码(pythonic),python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我有以下代码 lst = { 1: 39, 2: 2, 3: 1, 4: 21 } threshold_value=10 total_value=15 passed = { key:value for key, value in lst.items() if value <threshold_value} print(passed) 我将如何以紧凑的Python方式编写代码 非常感谢您的精彩推荐。这就是您想要的吗 [(v/total_value)*100 for k
lst = {
1: 39,
2: 2,
3: 1,
4: 21 }
threshold_value=10
total_value=15
passed = { key:value for key, value in lst.items() if value <threshold_value}
print(passed)
我将如何以紧凑的Python方式编写代码
非常感谢您的精彩推荐。这就是您想要的吗
[(v/total_value)*100 for k,v in lst.items() if v < threshold_value]
[(v/总值)*100表示k,v表示第一项中的v(),如果v<阈值]
这是代数替换:
passed = { key:value for key, value in lst.items() if value <threshold_value}
passed={key:value for key,value in lst.items(),如果value您打算对OP使用lst
作为字典变量名发表任何评论?
passed = { key:value for key, value in lst.items() if value <threshold_value}
passed = { key: (value/total_value)*100
for key, value in lst.items() if value <threshold_value}