简体   繁体   English

在输出中检索空列表而不是值

[英]Retrieving empty list in the output instead of values

I have the following code, I want to filter out those values which are less than the threshold value and store it in a variable.我有以下代码,我想过滤掉那些小于阈值的值并将其存储在一个变量中。 But the below code gives an empty list in the output.但是下面的代码在输出中给出了一个空列表。

current_memory_available=29.373699550744604
total_memory=614480960
lst= {0: 602112, 1: 12852224, 2: 12992768, 3: 3211264, 4: 6717952, 5: 7012864, 6: 1605632, 7: 4391936, 8: 5571584, 9: 5571584, 10: 802816, 11: 6326272, 12: 11044864, 13: 11044864, 14: 401408, 15: 9840640, 16: 9840640, 17: 9840640, 18: 100352, 19: 100352, 20: 411074560, 21: 67141632, 22: 16392000}


passed = {key: (value/total_memory) * 100 for key,value in lst.items() if value < 
current_memory_available}
print(passed)

Thanks, Help is highly appreciated谢谢,非常感谢帮助

'if' statement used in dict comprehension will never get True as all the values are greater than 'current_memory_available' variable. dict comprehension 中使用的“if”语句永远不会为 True,因为所有值都大于“current_memory_available”变量。 Thus the dictionary is getting printed as null.因此字典被打印为空值。

current_memory_available = 29.373699550744604
total_memory=614480960
dct = {0: 602112, 1: 12852224, 2: 12992768, 3: 3211264, 4: 6717952, 5: 7012864, 6: 1605632, 7: 4391936, 8: 5571584, 9: 5571584, 10: 802816, 11: 6326272, 12: 11044864, 13: 11044864, 14: 401408, 15: 9840640, 16: 9840640, 17: 9840640, 18: 100352, 19: 100352, 20: 411074560, 21: 67141632, 22: 16392000}
new_dct = {}
for key, value in dct.items():
    calc = (value / total_memory) *100
    if calc < current_memory_available:
        new_dct[key] = value
print(new_dct)

Below logic is incorrect:下面的逻辑不正确:

passed = {key: (value/total_memory) * 100 for key,value in lst.items() if value < 
current_memory_available}

Try using below:尝试使用以下:

passed = {key for key,value in lst.items() if (value/total_memory) * 100 < current_memory_available}

Full code:完整代码:

current_memory_available=29.373699550744604
total_memory=614480960
lst= {0: 602112, 1: 12852224, 2: 12992768, 3: 3211264, 4: 6717952, 5: 7012864, 6: 1605632, 7: 4391936, 8: 5571584, 9: 5571584, 10: 802816, 11: 6326272, 12: 11044864, 13: 11044864, 14: 401408, 15: 9840640, 16: 9840640, 17: 9840640, 18: 100352, 19: 100352, 20: 411074560, 21: 67141632, 22: 16392000}


passed = {key for key,value in lst.items() if (value/total_memory) * 100 < current_memory_available}
print(passed)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM