SQL Python如何扩展浮点数组



嗨,我得到了一个代码,它正在寻找我的SQL输出的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'没有else(扩展)的输出是:

{
"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中将度量附加到列表(而不是扩展),即更改:

vals['value'].extend(values)

vals['value'].append(measure)

最新更新