You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo11如何将同一变量输出的多个单元素列表合并为一个列表

问题出在这两点:
  1. 你把列表定义和打印逻辑都写在了for record in self的循环内部,每次循环都会新建一个独立的单元素列表,自然不会得到合并后的总列表
  2. 对chain.from_iterable的使用场景和参数理解错误:你在循环内部每次只传入了单个单元素列表['00:00'],该方法会遍历列表内的每一个元素(也就是字符串'00:00'),而字符串本身是可迭代对象,会被拆分为单个字符输出,自然得不到预期结果。
正确实现方案

方案1:最简洁的列表推导式(推荐)

直接遍历所有record提取字段值生成列表:

def _compute_sum_50(self):
    overtime_list = [record.overtime_50 for record in self]
    print(overtime_list)

方案2:常规循环追加写法

def _compute_sum_50(self):
    overtime_list = []
    for record in self:
        overtime_list.append(record.overtime_50)
    # 循环结束后再打印合并后的列表
    print(overtime_list)

方案3:如果你确实需要用chain.from_iterable

要先收集所有单元素列表,再统一调用处理:

from itertools import chain

def _compute_sum_50(self):
    all_single_lists = [[record.overtime_50] for record in self]
    overtime_list = list(chain.from_iterable(all_single_lists))
    print(overtime_list)

内容的提问来源于stack exchange,提问作者SaltySteven

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 18:27:05