【发布时间】:2021-06-19 10:23:05
【问题描述】:
我的目标是构建一个函数,最终用新元组替换字典中的元组。
目前我遇到的问题是小于 100 的低范围整数向下舍入到最接近的第 10 位。当我隔离函数时,我的 round_down 函数正确返回预期值。当我将 tup_update_sub 与字典进行比较时,1000 和 100 以上的整数会正确向下舍入并更新。当我测试低于 100 的音量时,比较返回 None,而不是正确的整数。
我目前的工作流程是: 接受从先前代码(卷)确定的整数> 有条件地选择一个除数> 向下取整> 匹配字典中的值> 从字典中返回值> 创建新元组> 替换字典中的元组
我在最后包含了用于调试的打印。我的沮丧是无法调试为什么字典在值低于 100 时无法正确引用。
#dictionary to be updated with new tuple from tup_update_sub
dict_tuberack1={tuberack1['A1']:(14000,104), tuberack1['A2']:(14000,104), tuberack1['A3']:(14000,104)}
#dictionary to referenced by key for new tuple
vol_tuberack1= {14000: 104, 13500: 101, 13000: 98, 12500: 94,
12000: 91, 11500: 88, 11000: 85, 10500: 81,
10000: 78, 9500: 75, 9000: 72, 8500: 68,
8000: 65, 7500: 62, 7000: 59, 6500: 55,
6000: 52, 5500: 49, 5000: 46, 4500: 42,
4000: 39, 3500: 36, 3000: 33, 2500: 29,
2000: 26, 1000: 20, 900: 15, 800: 15,
700: 15, 600: 15, 500: 15, 400: 13,
300: 11, 200: 9, 100: 6, 90: 2, 80: 2,
70: 2, 60: 2, 50: 1, 40: 1, 30: 1,
20: 1, 10: 1}
def round_down(volume,divisor):
vol_even=floor(volume/divisor)*divisor
return vol_even
def tup_update_sub(volume,dict_vol,dict_labware,labware,well):
tup = dict_labware.get(labware[well])
adj_list=list(tup)
adj_list[0]=volume
divisor=1
if volume >=1000:
divisor=1000
vol_even=round_down(volume, divisor)
elif volume <1000 >= 100:
divisor=100
vol_even=round_down(volume,divisor)
else:
divisor=10
vol_even=round_down(volume,divisor)
new_height=dict_vol.get(vol_even)
adj_list[1]=new_height
new_tup=tuple(adj_list)
#modified for debug
print(vol_even) #to show that volume was correctly rounded
print(new_tup) #to show that function has correctly chosen the intended values
print(dict_labware[labware[well]]) #to show that the correct destination was chosen
#so far the script has functioned great when it comes to updating dictionaries with new tuples. The challenge is getting the right value referenced when the rounded value is less than 100.
【问题讨论】:
-
您能否将您的代码编辑成一个最小的问题示例,并清楚地描述您期望得到的结果?我们没有那种 opentrons 的东西,而且它在这里似乎不是必需的。
-
感谢回复,已找到答案,代码已配对。
标签: python dictionary tuples rounding rounding-error