gpt4 book ai didi

python-3.x - 根据动态的、递增的日期变量创建多个字典

转载 作者:行者123 更新时间:2023-12-03 01:28:35 26 4
gpt4 key购买 nike

我有一个存储在变量 body 中的 elasticsearch body 字典:

body = {"fields": ["dates","c_u", "tawgs.db_id"], "query": { "filtered": { "query": { "match": { "dates": "2019-07-26" } }, "filter": { "terms": { "tawgs.db_id": products } } } } })

我的问题是在“匹配”字段中,我需要增加
{ "dates": "2019-07-26" }

根据用户输入的日期回溯,按天计算。

我开发了日期回溯变量,以根据用户输入的 # of days_back 输出可追溯到 # 天的日期列表。

在将这个日期列表单独集成到正文字典中以便在 Elasticsearch 中进行多次搜索时,我感到非常迷茫。

我开发日期列表的代码如下:
# dates desired outputted in a list
base = dt.date.today()
days_back = 5 # USER INPUT
date_list = [base - dt.timedelta(days=x) for x in reversed(range(days_back + 1))]
dates = ['{}-{}-{}'.format(y,m,d) for y, m, d in map(lambda x: str(x).split('-'), date_list)]

它输出列表: ['2019-07-21', '2019-07-22', '2019-07-23', '2019-07-24', '2019-07-25','2019-07-26']
所以我需要的是输出多个正文字典,如:
body = {"fields": ["dates","c_u", "tawgs.db_id"], "query": { "filtered": { "query": { "match": { "dates": "2019-07-26" } }, "filter": { "terms": { "tawgs.db_id": products } } } } })

body = {"fields": ["dates","c_u", "tawgs.db_id"], "query": { "filtered": { "query": { "match": { "dates": "2019-07-25" } }, "filter": { "terms": { "tawgs.db_id": products } } } } })

body = {"fields": ["dates","c_u", "tawgs.db_id"], "query": { "filtered": { "query": { "match": { "dates": "2019-07-24" } }, "filter": { "terms": { "tawgs.db_id": products } } } } })

body = {"fields": ["dates","c_u", "tawgs.db_id"], "query": { "filtered": { "query": { "match": { "dates": "2019-07-23" } }, "filter": { "terms": { "tawgs.db_id": products } } } } })

等等,只有日期字段发生变化。

请帮忙!我没有成功插入 for 循环。提前致谢!

最佳答案

创建一个没有显式 dates 的基本字典键/值对,然后在该查询的循环中为其分配所需的日期:

import datetime as dt
import copy

base = dt.date.today()
days_back = 5 # USER INPUT
date_list = [base - dt.timedelta(days=x) for x in reversed(range(days_back + 1))]

base_body = {"fields": ["dates","c_u", "tawgs.db_id"], "query": { "filtered": { "query": { "match": {} }, "filter": { "terms": { "tawgs.db_id": [] } } } }}

for date in date_list:
body = copy.deepcopy(base_body)
body['query']['filtered']['query']['match']['dates'] = date.strftime("%Y-%m-%d")
print(body)

结果:

{'fields': ['dates', 'c_u', 'tawgs.db_id'], 'query': {'filtered': {'query': {'match': {'dates': '2019/07/21'}}, 'filter': {'terms': {'tawgs.db_id': []}}}}}
{'fields': ['dates', 'c_u', 'tawgs.db_id'], 'query': {'filtered': {'query': {'match': {'dates': '2019/07/22'}}, 'filter': {'terms': {'tawgs.db_id': []}}}}}
{'fields': ['dates', 'c_u', 'tawgs.db_id'], 'query': {'filtered': {'query': {'match': {'dates': '2019/07/23'}}, 'filter': {'terms': {'tawgs.db_id': []}}}}}
{'fields': ['dates', 'c_u', 'tawgs.db_id'], 'query': {'filtered': {'query': {'match': {'dates': '2019/07/24'}}, 'filter': {'terms': {'tawgs.db_id': []}}}}}
{'fields': ['dates', 'c_u', 'tawgs.db_id'], 'query': {'filtered': {'query': {'match': {'dates': '2019/07/25'}}, 'filter': {'terms': {'tawgs.db_id': []}}}}}
{'fields': ['dates', 'c_u', 'tawgs.db_id'], 'query': {'filtered': {'query': {'match': {'dates': '2019/07/26'}}, 'filter': {'terms': {'tawgs.db_id': []}}}}}

关于python-3.x - 根据动态的、递增的日期变量创建多个字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57225300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com