繁体   English   中英

SQL Python 如何扩展浮点数组

[英]SQL Python how to extend float array

嗨,我得到了一个代码,它正在寻找我的 SQL output 的 id 并尝试像这样将具有相同 id 的措施的结果合并在一起

result = session.execute(stmt, stmt_args)
result2 = session.execute(stmt, stmt_args)
result3 = result2.fetchall()

for request_ts, identifier, ts, array_id, index, measures in result3:
        vals = vals_dict.get(array_id)
        if vals is None:
            # add record to index
            values = vals_dict[array_id] = {
                'id': array_id,
                'request_ts': request_ts,
                'identifier': identifier,
                'ts': ts,
                'value': measures,
                }
            # adds the same record to the relust list
            arrayvals.append(values)
        else:
            # # look up the record in the index
            vals['value'].extend(values)

所以我的问题是我的措施是浮动的。 所以我收到错误消息AttributeError: 'float' object has no attribute 'extend' My Output without the else (extend) 是:

{
    "result_array": [
        {
            "id": 1,
            "identifier": "PortionMotor01",
            "request_ts": "Mon, 22 Feb 2021 12:12:27 GMT",
            "ts": "Wed, 17 Feb 2021 12:12:00 GMT",
            "value": 17.1
        },
        {
            "id": 2,
            "identifier": "PortionMotor01",
            "request_ts": "Mon, 22 Feb 2021 12:12:27 GMT",
            "ts": "Wed, 17 Feb 2021 12:14:00 GMT",
            "value": 2005.0
        }
    ]
}

但是结果中每个 id 只有 1 个度量值。 我要这个:

{
    "result_array": [
        {
            "id": 1,
            "identifier": "PortionMotor01",
            "request_ts": "Mon, 22 Feb 2021 12:12:27 GMT",
            "ts": "Wed, 17 Feb 2021 12:12:00 GMT",
            "value": [17.1, 18.0, ...]
        },
        {
            "id": 2,
            "identifier": "PortionMotor01",
            "request_ts": "Mon, 22 Feb 2021 12:12:27 GMT",
            "ts": "Wed, 17 Feb 2021 12:14:00 GMT",
            "value": [2005.0, 123.5, ...]
        }
    ]
}

我唯一的问题是扩展...有人可以帮助我吗?

valsNone时,将'value'的值设为包含度量的列表,更改

'value': measures,

'value': [measures],

现在您将拥有一个包含一个浮点值的列表。 然后在else append 中对列表的度量(而不是扩展),即更改:

        vals['value'].extend(values)

        vals['value'].append(measure)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM