Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有没有可能像这样轻松格式化浮点数:
f"{value: .2f}"
在打印dict时,包含那些浮点数作为值?这不起作用,但我正在寻找等效的东西:
f"{dictionary: .2f}"
我假设您在迭代时尝试在 dict 中打印值。如果是这种情况,这应该做你想做的事情:
for key, value in dictionary.items(): print("{:.2f}".format(value))
您可以循环字典的键以打印每个值(或将它们组合成一个字符串进行打印)。您可以使用以下命令循环每个键:
for key in dictio.keys(): print (key, "{:.2f}".format(dictio[key]))