大家好,我用python编写了一个SQL stmt,其中包含测量结果的输出值。每个度量值都有唯一的id和时间戳。我现在的输出是这样的。输出的Pythoncode:
arrayvals = []
for row in result:
arrayvals.append({
'request_ts': row[0],
'identifier': row[1],
'ts': row[2],
'value': row[5],
})
return (arrayvals)OutPut:
{
"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 code:
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':措施,vals‘'value’.append(度量)
现在可以工作了谢谢大家
发布于 2021-02-22 16:00:45
有没有可能“组合”这些id,以生成最后的输出?
最好的选择是修复sql语句,一开始就生成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)第三个最好的选择,如果判决是不可能的,那就是...对列表执行相同的操作,如果列表中已经添加了id为的记录,请进行查找。
如果列表中的条目数量很多,我仍然建议使用字典作为索引:在字典中查找记录是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)https://stackoverflow.com/questions/66311837
复制相似问题