使用for循环更改字典值



我有一个想要修改的字典。不是字符串,只有数字。下面的函数应该将字典中的所有数字转换为两个十进制浮点数:

def roundup(resultdict):
print("resultdict is: " +str(resultdict))
for item in resultdict.items():
print("item is: " + str(item))
if isinstance(item[1], str):
print("The item {} is a string {}".format(item[0], item[1]))
else:
item = list(item) # else Python gives an error it cannot modify a tuple
item[1] = float("{:.2f}".format(item[1]))
print("The item {} is now a float {}".format(item[0], item[1]))
return resultdict
newdict = roundup(resultdict)
print("newdict is: " + str(newdict))

这似乎工作(虽然4改为4.0,而不是4.00),但返回原始输入?参见下面输出中的newdict。

resultdict is: {'standplaats': 'Zagreb', 'totaal': 4215.04, 'totaal_v': 2087.43, 'totaaleenmalig': 16834.78, 'zone': 4, 'categorie': 'A', 'KKC': 0.961, 'ns': 2127.61, 'bs': 2677.58, 'KKC_s': -40.66, 'spvm': 751.82, 'kkc_spvm': -14.66, 'spvp': 526.28, 'kkc_spvp': -13.34, 'spvk': 0.0, 'kkc_spvk': -0.0, 'vh': 502, 'prm': 195, 'kkc_prm': -1.9, 'prp': 68.25, 'kkc_prp': -0.67, 'ovp': 463, 'tv': 314, 'ih': -497.86, 'ibkh': -163.83, 'hk': 3470.14, 'av': 13364.64}
item is: ('standplaats', 'Zagreb')
The item standplaats is a string Zagreb
item is: ('totaal', 4215.04)
The item totaal is now a float 4215.04
item is: ('totaal_v', 2087.43)
The item totaal_v is now a float 2087.43
item is: ('totaaleenmalig', 16834.78)
The item totaaleenmalig is now a float 16834.78
item is: ('zone', 4)
The item zone is now a float 4.0
item is: ('categorie', 'A')
The item categorie is a string A
item is: ('KKC', 0.961)
The item KKC is now a float 0.96
item is: ('ns', 2127.61)
The item ns is now a float 2127.61
item is: ('bs', 2677.58)
The item bs is now a float 2677.58
item is: ('KKC_s', -40.66)
The item KKC_s is now a float -40.66
item is: ('spvm', 751.82)
The item spvm is now a float 751.82
item is: ('kkc_spvm', -14.66)
The item kkc_spvm is now a float -14.66
item is: ('spvp', 526.28)
The item spvp is now a float 526.28
item is: ('kkc_spvp', -13.34)
The item kkc_spvp is now a float -13.34
item is: ('spvk', 0.0)
The item spvk is now a float 0.0
item is: ('kkc_spvk', -0.0)
The item kkc_spvk is now a float -0.0
item is: ('vh', 502)
The item vh is now a float 502.0
item is: ('prm', 195)
The item prm is now a float 195.0
item is: ('kkc_prm', -1.9)
The item kkc_prm is now a float -1.9
item is: ('prp', 68.25)
The item prp is now a float 68.25
item is: ('kkc_prp', -0.67)
The item kkc_prp is now a float -0.67
item is: ('ovp', 463)
The item ovp is now a float 463.0
item is: ('tv', 314)
The item tv is now a float 314.0
item is: ('ih', -497.86)
The item ih is now a float -497.86
item is: ('ibkh', -163.83)
The item ibkh is now a float -163.83
item is: ('hk', 3470.14)
The item hk is now a float 3470.14
item is: ('av', 13364.64)
The item av is now a float 13364.64
newdict is: {'standplaats': 'Zagreb', 'totaal': 4215.04, 'totaal_v': 2087.43, 'totaaleenmalig': 16834.78, 'zone': 4, 'categorie': 'A', 'KKC': 0.961, 'ns': 2127.61, 'bs': 2677.58, 'KKC_s': -40.66, 'spvm': 751.82, 'kkc_spvm': -14.66, 'spvp': 526.28, 'kkc_spvp': -13.34, 'spvk': 0.0, 'kkc_spvk': -0.0, 'vh': 502, 'prm': 195, 'kkc_prm': -1.9, 'prp': 68.25, 'kkc_prp': -0.67, 'ovp': 463, 'tv': 314, 'ih': -497.86, 'ibkh': -163.83, 'hk': 3470.14, 'av': 13364.64}

我做错了什么?任何帮助都非常感激!

谢谢你的建议。解决方案是遍历键、值对,并使用值而不是迭代器修改字典键。四舍五入现在也固定了。

def roundup(resultdict):
print("resultdict is: " +str(resultdict))
for key, value in resultdict.items():
print("item is: " + str(key) + str(value))
if isinstance(value, str):
print("The item {} is a string {}".format(key, value))
else:
resultdict[key] = "{:.2f}".format(round(value, 2))
print("The item {} is now a float {}".format(key, value))
return resultdict
newdict = roundup(resultdict)
print("newdict is: " + str(newdict))

相关内容

  • 没有找到相关文章

最新更新