给出的字典:
weights = {"A":16, "B": 3, "C": 5)
我想要这些值的倒数。
输出:
weights_dict_reci = {"A":0,0625, "B": 0,3333333, "C": 0,2)
到目前为止我尝试过:
weights_dict_reci = {value: 1 / weights_dict[value] for value in weights_dict}
和
for key in weights_dict:
weights_dict[key] = 1 / weights_dict[key]
每次我收到错误:/不支持的操作数类型:'int'和'function'
第一个:如何使 tke 为 dict 值的倒数?
第二:我的代码中出现此错误的原因是什么?
谢谢!
你可以尝试:
weights = {"A":16, "B": 3, "C": 5}
{k:1/v for k,v in weights.items()}