为什么这个除法的结果返回一个整数而不是一个浮点数?
Why is the result of this division returning a integer not a float?
我正在尝试在两个 dict
之间进行除法运算,但我总是得到 int
结果,但我希望得到 float
.
这是正在发生的事情:
test_dict1 = {'gfg': 20, 'is': 24, 'best': 30}
test_dict2 = {'gfg': 7, 'is': 7, 'best': 7}
res = {key: test_dict1[key] // test_dict2.get(key, 0)
for key in test_dict1.keys()}
res = {'best': 4, 'gfg': 2, 'is': 3}
您的接线员有问题://
。 python 中的 //
运算符 returns 除法的商。以'best'键为例,30 // 7 => 4Q, 2R。您正在寻找的只是 /
运算符。 30/7 = 4.285.....
更正:
res = {
key: test_dict1[key] / test_dict2.get(key, 0) for key in test_dict1.keys()
}
我正在尝试在两个 dict
之间进行除法运算,但我总是得到 int
结果,但我希望得到 float
.
这是正在发生的事情:
test_dict1 = {'gfg': 20, 'is': 24, 'best': 30}
test_dict2 = {'gfg': 7, 'is': 7, 'best': 7}
res = {key: test_dict1[key] // test_dict2.get(key, 0)
for key in test_dict1.keys()}
res = {'best': 4, 'gfg': 2, 'is': 3}
您的接线员有问题://
。 python 中的 //
运算符 returns 除法的商。以'best'键为例,30 // 7 => 4Q, 2R。您正在寻找的只是 /
运算符。 30/7 = 4.285.....
更正:
res = {
key: test_dict1[key] / test_dict2.get(key, 0) for key in test_dict1.keys()
}