【发布时间】:2021-06-23 03:42:56
【问题描述】:
数据示例:
response = {
"took" : value1,
"_shards" : {
"total" : 2,
},
"hits" : {
"total" : {
"value" : 150,
},
"hits" : [
{
"_index" : "index1",
"_type" : "_doc",
"_source" : {
"date" : "date1",
"hit" : 1,
"routing-key" : "id_key1",
"data": vector1[0:299]
},
},
{
"_index" : "index2",
"_type" : "_doc",
"_source" : {
"date" : "date2",
"hit" : 2,
"routing-key" : "id_key2",
"data": vector2[0:299]
},
},
{
"_index" : "index3",
"_type" : "_doc",
"_source" : {
"date" : "date3",
"hit" : 3,
"routing-key" : "id_key3",
"data": vector3[0:299]
},
},
#...
# I am not going to copy the whole request but there are until 150 hits
#...
]
}
}
现在我想从请求中的所有命中中获取位置 120 的“数据”的值:vector[0:299]
我已经尝试过了
vect_sol = response['hits']['hits'][:]['_source']['data'][120]
但我得到了错误
TypeError: list indices must be integers or slices, not str
要获取我使用过的“命中”字典中的索引
vect_sol = response['hits']['hits'][:]
它有效。那么我如何通过for 循环在数据向量中获得所需的值
for i in range(hits):
data_sol[i] = response['hits']['hits'][i]['_source']['data'][120]
这很好用,但是当数据请求由 10,000 次或更多(可能更大)组成时,脚本需要时间来填充 data_sol 向量。
我猜测是否有某种功能或不同的方式可以将数据作为请求获取,但会缩短脚本的执行时间。
【问题讨论】:
标签: performance dictionary for-loop python-3.9