首页 > 解决方案 > Python SQL 生成值列表

问题描述

嗨,伙计们,在 python 中得到了一个 SQL stmt,其中包含测量值的输出。每个 Measure 都有一个唯一的 id 和时间戳。我现在的输出看起来像这样。输出的 Python 代码:

arrayvals = []
    for row in result:
        arrayvals.append({
            'request_ts': row[0],
            'identifier': row[1],
            'ts': row[2],
            'value': row[5],
            })
   
    return (arrayvals) 

输出:

{
    "result_array": [
        {
            "identifier": "1",
            "request_ts": "Mon, 22 Feb 2021 08:43:03 GMT",
            "ts": "Wed, 17 Feb 2021 12:12:00 GMT",
            "value": 17.1
        },
        {
            "identifier": "1",
            "request_ts": "Mon, 22 Feb 2021 08:43:03 GMT",
            "ts": "Wed, 17 Feb 2021 12:12:00 GMT",
            "value": 18.0
        },
....

我想要的是每个 id 只有 1 个输出,其中包含列表中的值。我试图用 fetchall() 来实现这一点,但现在它得到所有输出的所有值,如下所示: Pyhton 代码:

result2 = result.fetchall()
for row in result:
        arrayvals.append({
            'request_ts': row[0],
            'identifier': row[1],
            'ts': row[2],
            'value': [i[5] for i in result2],
            })

输出:

"result_array": [
        {
            "identifier": "01",
            "request_ts": "Mon, 22 Feb 2021 08:46:48 GMT",
            "ts": "Wed, 17 Feb 2021 12:12:00 GMT",
            "value": [
                17.1,
                18.0,
                2005.0,
                17000.0,
                1234.0
            ]
        }
]

但我想要这样:

"result_array": [
        {
            "identifier": "01",
            "request_ts": "Mon, 22 Feb 2021 08:46:48 GMT",
            "ts": "Wed, 17 Feb 2021 12:12:00 GMT",
            "value": [
                17.1,
                18.0
            ]
        }
{
            "identifier": "02",
            "request_ts": "Mon, 22 Feb 2021 08:46:48 GMT",
            "ts": "Wed, 17 Feb 2021 12:12:00 GMT",
            "value": [
                2005.0,
                17000.0,
                1234.0
            ]
        }
]

是否可以“组合” id 来生成最后一个输出?差点忘了我的 SQL 是这样的:

SELECT
    NOW() AS request_ts,
    I.name AS identifier,
    AI.ts,
    AD.array_info_id,
    AD.index,
    AD.value AS measures
FROM
    machine M
INNER JOIN identifier I
    ON
    I.machine_id = M.id
INNER JOIN PDA_ArrayInfo AI
    ON
    I.id = AI.identifier_id
INNER JOIN PDA_ArrayData AD
    ON
    AD.array_info_id = AI.id
WHERE
    M.serial = :machine_serial_arg

来自 Masklinn 的回答:

for request_ts, identifier, ts, array_id, index, measures in result2.fetchall():
        vals = vals_dict.get(array_id)
        if vals is None:
            # add record to index
            values = vals_dict[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'].append(measures)
return(values)

改变了两件事:'value': [measures], vals['value'].append(measures)

现在它的工作谢谢大家

标签: pythonsql

解决方案


是否可以“组合” id 来生成最后一个输出?

最好的选择是修复您的 sql 语句以生成该语句。由于您不显示 SQL,因此很难在此处提供帮助。

第二个最佳选择是将数据存储为数据的映射identifier这样您可以使用该get方法检查该标识符是否已有数据,如果没有则创建新数据,否则您只需附加到现有value数组:

for req_ts, ident, ts, _, _, values in result.fetchall():
    vals = vals_dict.get(ident)
    if vals is None:
        vals_dict[ident] = {
            'request_ts': req_ts,
            'identifier': ident,
            'ts': ts,
            'value': values,
        }
    else:
        vals['value'].extend(values)

第三个最好的选择,如果 dict 是不可能的,是......对列表做同样的事情,如果已经有一条记录添加到列表中,则查找 id。

如果列表中的条目数可能很大,我仍然建议使用 dict 作为索引:在 dict 中查找记录是 O(1) 操作,在列表中是 O(n),导致二次行为. 如果您只有十几个条目,这不是问题,如果您有数千个条目,则是一个大问题。

for req_ts, ident, ts, _, _, values in result.fetchall():
    vals = vals_dict.get(ident)
    if vals is None:
        # adds the record to the index
        values = vals_dict[ident] = {
            'request_ts': req_ts,
            'identifier': ident,
            'ts': ts,
            'value': values,
        }
        # adds the same record to the results list
        arrayvals.append(values)
    else:
        # just look up the record in the index
        vals['value'].extend(values)

推荐阅读