在字典中查找值,如果键不存在则返回NA。

huangapple go评论130阅读模式
英文:

find value or NA if the key does not exist in dict of dicts

问题

The code you provided seems to be written in Python and appears to have HTML entities like " instead of double quotes (") for string representation. Here's the corrected code with the translation and without any extra information:

结果 = {
    "IPSEC-IMIX": {
        "吞吐量": 5974,
        "kpps": 2114,
        "速率": 31.56,
        "qfp": 86,
        "标签": "BLD_V179_THROTTLE_LATEST_20230513_170843"
    },
    "IPSEC_QOS_DPI_FNF-IMIX": {
        "吞吐量": 5913,
        "kpps": 2065,
        "速率": 31.223,
        "qfp": 90,
        "标签": "BLD_V179_THROTTLE_LATEST_20230513_170843"
    },
    "IPSEC_QOS_DPI_FNF_SNAT_ZBFW-IMIX": {
        "吞吐量": 2640,
        "kpps": 922,
        "速率": 13.942,
        "qfp": 85,
        "标签": "BLD_V179_THROTTLE_LATEST_20230513_170843"
    },
    "IPSEC_QOS_DPI_FNF_TNAT-IMIX": {
        "吞吐量": 5884,
        "kpps": 2055,
        "速率": 31.067,
        "qfp": 95,
        "标签": "BLD_V179_THROTTLE_LATEST_20230513_170843"
    },
    "IPSEC_MCAST-IMIX": {
        "吞吐量": 3330,
        "kpps": 1163,
        "速率": 35.164,
        "qfp": 64,
        "标签": "BLD_V179_THROTTLE_LATEST_20230513_170843"
    },
    "IPSEC_QOS_DPI_FNF_MCAST-IMIX": {
        "吞吐量": 3407,
        "kpps": 1190,
        "速率": 36.002,
        "qfp": 87,
        "标签": "BLD_V179_THROTTLE_LATEST_20230513_170843"
    },
    "IPSECV6-IMIX": {
        "吞吐量": 5673,
        "kpps": 1919,
        "速率": 29.904,
        "qfp": 25,
        "标签": "BLD_V179_THROTTLE_LATEST_20230513_170843"
    },
    "IPSECV6_QOSV6_DPI_FNF-IMIX": {
        "吞吐量": 5768,
        "kpps": 1951,
        "速率": 30.403,
        "qfp": 38,
        "标签": "BLD_V179_THROTTLE_LATEST_20230513_170843"
    }
}

the_feature = "IPSEC-IMIX"  # Replace this with the feature you want to check

答案 = 结果.get(the_feature, {}).get("吞吐量", "NA")

This code will retrieve the "吞吐量" (throughput) for the specified feature, or return "NA" if the feature does not exist in the dictionary.

英文:
  results = {
"IPSEC-IMIX": {
"throughput": 5974,
"kpps": 2114,
"rate": 31.56,
"qfp": 86,
"label": "BLD_V179_THROTTLE_LATEST_20230513_170843"
},
"IPSEC_QOS_DPI_FNF-IMIX": {
"throughput": 5913,
"kpps": 2065,
"rate": 31.223,
"qfp": 90,
"label": "BLD_V179_THROTTLE_LATEST_20230513_170843"
},
"IPSEC_QOS_DPI_FNF_SNAT_ZBFW-IMIX": {
"throughput": 2640,
"kpps": 922,
"rate": 13.942,
"qfp": 85,
"label": "BLD_V179_THROTTLE_LATEST_20230513_170843"
},
"IPSEC_QOS_DPI_FNF_TNAT-IMIX": {
"throughput": 5884,
"kpps": 2055,
"rate": 31.067,
"qfp": 95,
"label": "BLD_V179_THROTTLE_LATEST_20230513_170843"
},
"IPSEC_MCAST-IMIX": {
"throughput": 3330,
"kpps": 1163,
"rate": 35.164,
"qfp": 64,
"label": "BLD_V179_THROTTLE_LATEST_20230513_170843"
},
"IPSEC_QOS_DPI_FNF_MCAST-IMIX": {
"throughput": 3407,
"kpps": 1190,
"rate": 36.002,
"qfp": 87,
"label": "BLD_V179_THROTTLE_LATEST_20230513_170843"
},
"IPSECV6-IMIX": {
"throughput": 5673,
"kpps": 1919,
"rate": 29.904,
"qfp": 25,
"label": "BLD_V179_THROTTLE_LATEST_20230513_170843"
},
"IPSECV6_QOSV6_DPI_FNF-IMIX": {
"throughput": 5768,
"kpps": 1951,
"rate": 30.403,
"qfp": 38,
"label": "BLD_V179_THROTTLE_LATEST_20230513_170843"
}
}

I have a string the_feature which maps to to the keys in the above dict (example IPSEC-IMIX) and I want to get either the throughput or "NA" if the feature does not exist.

Tried the below but it does not work when an inexistent the_feature is used.

answer = results[the_feature]["throughput"] if results[the_feature]["throughput"] else "NA"
Traceback (most recent call last):
File "C:\code\api_server_imix.py", line 1113, in get_results
item["release_speed"] = release_obj.results[the_feature].get("throughput", "NA")
value = super().__getitem__(key)
^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'IPSEC-1400'

答案1

得分: 1

以下是翻译好的部分:

这是一个更加简洁的示例,也许是您正在寻找的。关键的技巧是您只需要检查键是否存在,而在您的示例中它始终存在 - 此外,在您的代码中,您不是检查键的存在,而是检查它是 true 还是 false(大多数类型的大多数值都会返回 true)。

results = {
    "IPSEC-IMIX": {
        "throughput": 5974
    },
    "IPSEC_QOS_DPI_FNF-IMIX": {
        "throughput": 5913
    },
    "IPSEC_QOS_DPI_FNF_SNAT_ZBFW-IMIX": {
        "throughput": 2640
    },
    "NO_THROUGHPUT": {
        "other": 0
    }
}
features = results.keys()
for the_feature in features:
    answer = results[the_feature]["throughput"] if "throughput" in results[the_feature] else "NA"
    print(answer)

"""prints
5974
5913
2640
NA
"""
英文:

Here's a more minimal example and maybe what you're looking for. The trick is you just need to check to see if the key exists or not, and in your example it always existed -- plus, in your code you don't check for the key's existence but rather whether it is true or false (most values of most types return as true).

results = {
"IPSEC-IMIX": {
"throughput": 5974
},
"IPSEC_QOS_DPI_FNF-IMIX": {
"throughput": 5913
},
"IPSEC_QOS_DPI_FNF_SNAT_ZBFW-IMIX": {
"throughput": 2640
},
"NO_THROUGHPUT": {
"other": 0
}
}
features = results.keys()
for the_feature in features:
answer = results[the_feature]["throughput"] if "throughput" in results[the_feature] else "NA"
print(answer)
"""prints
5974
5913
2640
NA
"""

答案2

得分: 0

你可以使用 dict.get 来在字典中键不存在时提供默认结果(而不是出现 KeyError),并将它们链接在一起:

answer = results.get(the_feature, {}).get("throughput", "NA")
英文:

You can use dict.get to provide a default result when a key is not present in the dictionary (instead of getting a KeyError), and chain them together:

answer = results.get(the_feature,{}).get("throughput","NA")

答案3

得分: 0

以下是翻译好的内容:

要控制吞吐量的来源,您可以使用字典推导式与get()方法:

output = {x:results[x].get('throughput','NA') for x in results}

返回:

{'IPSEC-IMIX': 5974,
 'IPSEC_QOS_DPI_FNF-IMIX': 5913,
 'IPSEC_QOS_DPI_FNF_SNAT_ZBFW-IMIX': 2640,
 'IPSEC_QOS_DPI_FNF_TNAT-IMIX': 5884,
 'IPSEC_MCAST-IMIX': 3330,
 'IPSEC_QOS_DPI_FNF_MCAST-IMIX': 3407,
 'IPSECV6-IMIX': 5673,
 'IPSECV6_QOSV6_DPI_FNF-IMIX': 5768}

或者,如果您只想要值的列表:

output.values()

返回:

dict_values([5974, 5913, 2640, 5884, 3330, 3407, 5673, 5768])
英文:

In order to control the source of the throughput, you could use a dict comprehension with get():

output = {x:results[x].get('throughput','NA') for x in results}

Returning:

{'IPSEC-IMIX': 5974,
'IPSEC_QOS_DPI_FNF-IMIX': 5913,
'IPSEC_QOS_DPI_FNF_SNAT_ZBFW-IMIX': 2640,
'IPSEC_QOS_DPI_FNF_TNAT-IMIX': 5884,
'IPSEC_MCAST-IMIX': 3330,
'IPSEC_QOS_DPI_FNF_MCAST-IMIX': 3407,
'IPSECV6-IMIX': 5673,
'IPSECV6_QOSV6_DPI_FNF-IMIX': 5768}

Or if you only want the values as list:

 output.values()

Returning:

dict_values([5974, 5913, 2640, 5884, 3330, 3407, 5673, 5768])

答案4

得分: 0

你的问题不是解决方案本身,而是你没有事先检查键是否存在。

保留你的代码的基础上,你可以这样修改它使其工作:

if the_feature in results:
    answer = results[the_feature]["throughput"] if "throughput" in results[the_feature] else "NA"
英文:

your problem is not your solution by itself but the fact that you don't check wether the key exist beforehand

Keeping somewhat your code, you could do this to modify it and make it work

if the_feature in results:
answer = results[the_feature]["throughput"] if "throughput" in results[the_feature] else "NA"

huangapple
  • 本文由 发表于 2023年6月8日 19:53:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定