Python For循环处理数组时特定元素重复渲染问题求助
算术表达式排版函数重复渲染问题排查
在实现算术表达式排版功能的arithmetic_arranger函数时,遇到以下问题:
- 使用for循环处理算术表达式数组时,第一个和最后一个元素会被重复渲染(输出两次)
- 该问题仅在第一个数字的长度(len_a)大于第二个数字长度(len_b)和计算结果长度(len_c)时触发
原代码
def arithmetic_arranger(problems): top = "" middle = "" lines = "" bottom = "" for problem in problems: #split em up nums = problem.split(" ") first_num = nums[0] symbol = nums[1] last_num = nums[2] if len(first_num) > 4 < len(last_num): return "gk boleh lebih dr 4 yhhhh" #find the total if symbol == "+": total = str(int(first_num) + int(last_num)) elif symbol == "-": total = str(int(first_num) - int(last_num)) else: return "cuma boleh + or - tolol" #find the distances for the top line len_a = len(first_num) len_b = len(last_num) len_c = len(total) maxi = max(len_a, len_b, len_c) lines = lines + "--"+"-" * maxi + " " if maxi == len_a: top = top + " " + first_num + " " middle = middle + symbol + " " + " " * (len_a - len_b) + last_num + " " bottom = bottom + " " + " " * (len_a - len_c) + total + " " if maxi == len_b: top = top + " " + " " * (len_b - len_a) + first_num + " " middle = middle + symbol + " " + last_num + " " bottom = bottom + " " + " " * (len_b - len_c) + total + " " else: top = top + " " + " " * (len_c - len_a) + first_num + " " middle = middle + symbol + " " + " " * (len_c - len_b) + last_num + " " bottom = bottom + " " + total + " " #assemble it joint = top, middle, lines, bottom answer = '\n'.join(joint) return answer print(arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"]))
问题原因
核心问题是条件分支逻辑错误:
代码中使用两个独立的if语句判断maxi == len_a和maxi == len_b,当maxi == len_a时,第一个if分支执行完毕后,因maxi == len_b不成立,会直接进入else分支,导致同一组表达式的内容被两次追加到top、middle、bottom字符串中,最终出现重复渲染。
修复方案
将第二个if改为elif,让三个分支互斥,确保每个循环周期仅执行一个分支:
# 替换原条件判断部分 if maxi == len_a: top = top + " " + first_num + " " middle = middle + symbol + " " + " " * (len_a - len_b) + last_num + " " bottom = bottom + " " + " " * (len_a - len_c) + total + " " elif maxi == len_b: top = top + " " + " " * (len_b - len_a) + first_num + " " middle = middle + symbol + " " + last_num + " " bottom = bottom + " " + " " * (len_b - len_c) + total + " " else: top = top + " " + " " * (len_c - len_a) + first_num + " " middle = middle + symbol + " " + " " * (len_c - len_b) + last_num + " " bottom = bottom + " " + total + " "
同时将所有表达式末尾的" "(5个空格)改为" "(4个空格),让排版更贴合期望效果。
修复后完整代码
def arithmetic_arranger(problems): top = "" middle = "" lines = "" bottom = "" for problem in problems: #split em up nums = problem.split(" ") first_num = nums[0] symbol = nums[1] last_num = nums[2] if len(first_num) > 4 or len(last_num) > 4: return "数字长度不能超过4位" #find the total if symbol == "+": total = str(int(first_num) + int(last_num)) elif symbol == "-": total = str(int(first_num) - int(last_num)) else: return "仅支持+和-运算" #find the distances for the top line len_a = len(first_num) len_b = len(last_num) len_c = len(total) maxi = max(len_a, len_b, len_c) lines = lines + "--"+"-" * maxi + " " if maxi == len_a: top = top + " " + first_num + " " middle = middle + symbol + " " + " " * (len_a - len_b) + last_num + " " bottom = bottom + " " + " " * (len_a - len_c) + total + " " elif maxi == len_b: top = top + " " + " " * (len_b - len_a) + first_num + " " middle = middle + symbol + " " + last_num + " " bottom = bottom + " " + " " * (len_b - len_c) + total + " " else: top = top + " " + " " * (len_c - len_a) + first_num + " " middle = middle + symbol + " " + " " * (len_c - len_b) + last_num + " " bottom = bottom + " " + total + " " #assemble it joint = top.rstrip(), middle.rstrip(), lines.rstrip(), bottom.rstrip() answer = '\n'.join(joint) return answer print(arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"]))
(额外优化:将返回提示改为中文,同时用rstrip()去除末尾多余空格,避免排版出现不必要的空白)
内容的提问来源于stack exchange,提问作者user22311919
相关产品推荐
相关产品推荐

