在 PYTHON 中,如何将 listA[] 的每个索引值附加到 ListB[] 的每个索引值。请注意,两个列表具有相同的大小/长度
我是Python新手。请帮我。
示例: 我有 2 个不同的列表 indexnameList
和 payloadList
。每个包含 2 个值。长度/大小= 2。如下所示。
indexnameList = ['c_aa98efb4-630a-4eef-b51e-ba884635eehh_driver', 'c_aa98efb4-630a-4eef-b51e-ba884635eekk_driver']`
payloadList = [
{
"id": "d90b6782-0a94-4a0d-ab6a-42d95d47db40",
"version": "2.4",
"sequence": "1.0",
"event_id": "c21aebf8b235459180953d14bbf32027"
},
{
"id": "d90b6782-0a94-4a0d-ab6a-42d95d47db41",
"version": "2.5",
"sequence": "1.0",
"event_id": "3127eda13f93464c9ecc9dc7452a5c04"
}
]
我想将indexnameList的每个索引值附加到payloadList值。所以使用如下。 但始终只有在附加到 PayloadList 后才能获取第 '0' 个索引值。
body = []
c=0
for entry in payloadList:
body.append({'index': {'_index': indexnameList[c], '_type': 'doc', '_id': entry['id']}})
body.append(entry)
response = es_client.bulk(body=body)
return {"statusCode" :200,
"message" : str(len(payloadList)) + " objects inserted into opensearch.",
"open search response" :response,
"payloadList" : payloadList
}
需要解决方案 - 我如何在 for 循环内传递 indexnameList[c] 的值。所以各指标值 indexnameList 将在下面的行中追加。
body.append({'index': {'\_index': **indexnameList\[c\]**, '\_type': 'doc', '\_id': entry\['id'\]}})
I am new to Python. Please help me.
Example :
I have 2 different lists indexnameList
and payloadList
. Each contains 2 values. length/size= 2. Given below.
indexnameList = ['c_aa98efb4-630a-4eef-b51e-ba884635eehh_driver', 'c_aa98efb4-630a-4eef-b51e-ba884635eekk_driver']`
payloadList = [
{
"id": "d90b6782-0a94-4a0d-ab6a-42d95d47db40",
"version": "2.4",
"sequence": "1.0",
"event_id": "c21aebf8b235459180953d14bbf32027"
},
{
"id": "d90b6782-0a94-4a0d-ab6a-42d95d47db41",
"version": "2.5",
"sequence": "1.0",
"event_id": "3127eda13f93464c9ecc9dc7452a5c04"
}
]
I want to append the indexnameList each index value to the payloadList value. So using as below.
But always getting the '0'th index value only after appending to the payloadList .
body = []
c=0
for entry in payloadList:
body.append({'index': {'_index': indexnameList[c], '_type': 'doc', '_id': entry['id']}})
body.append(entry)
response = es_client.bulk(body=body)
return {"statusCode" :200,
"message" : str(len(payloadList)) + " objects inserted into opensearch.",
"open search response" :response,
"payloadList" : payloadList
}
Need solution -- How can i pass value of indexnameList[c] inside the for loop. So the each index value of
indexnameList will get in the below line to append.
body.append({'index': {'\_index': **indexnameList\[c\]**, '\_type': 'doc', '\_id': entry\['id'\]}})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想看看 zip() ,它的工作方式如下:
当然,它作为列表理解更漂亮:
无论如何:您当然也可以循环遍历多个列表,其中一个列表包含字典并更新它:)。
替代方法:
最后但并非最不重要的一点是:请尝试优化您的代码:D。 pep8 并使用 SO 代码块。
You might wanna look into zip() which works like this:
Of course it's more beautiful as list comprehension:
Anyway: you can of course also loop through multiple lists where one contains a dictionary and update that :).
Alternative approach:
Last but not least: please try to optimize your code :D. pep8 and using SO code-blocks.
如果我正确阅读您的问题,我认为您正在尝试将“indexnameList”中的每个元素添加到“payloadList”中每个字典中的相应键。如果这就是您想要做的,您可以迭代 indexnameList 并枚举它,以利用它们的索引,以便引用 PayloadList 中的相同索引。
如果您要打印出payloadList,它现在将包含一个名为“index_name”的新键,其中每个元素将包含indexnameList 值的值。
我还建议您了解 python 中的数据结构,特别是列表和字典,以及如何利用 for 循环。如果这不是您想要做的,请告诉我并给我更简洁的说明,以便我可以进一步帮助您。
If I am reading your question correctly, I think you are trying to add each element from the "indexnameList" to a respective key in each dictionary in the "payloadList". If this is what you are trying to do, you can iterate through the indexnameList and enumerate it, to utilize their indices, so as to reference that same index in the payloadList.
If you were to print out the payloadList, it will now contain a new key called "index_name", which will contain the value of the indexnameList value, for each element.
I would also advise you to learn about data structures in python, particularly lists and dictionaries, as well as how to utilize for loops. If this isn't what you were trying to do, please let me know and give me more concise instructions so I can help you further.