如何匹配和修改字典数组?

我目前正试图互相匹配和修改两个字典。在较高的层次上,我对每个问题都有一系列可能的answer_ids字典:

[{'2194241222': 'Not at all likely - 0','2194241223': '1','2194241224': '2','2194241391': '3','2194241392': '4','2194241393': '5','2194241394': '6','2194241395': '7','2194241396': '8','2194241397': '9','2194241398': 'Extremely likely - 10'},{'2194241407': 'Very satisfied','2194241408': 'Somewhat satisfied','2194241409': 'Neither satisfied nor dissatisfied','2194241410': 'Somewhat dissatisfied','2194241411': 'Very dissatisfied'},{'2194241444': 'Extremely well','2194241445': 'Very well','2194241446': 'Somewhat well','2194241447': 'Not so well','2194241448': 'Not at all well'}

以及一系列实际答案:

{'answers': [{'choice_id': '2194241397','row_id': '2194241221'}],'id': '331799110'},{'answers': [{'choice_id': '2194241408'}],'id': '331799114'},{'answers': [{'choice_id': '2194241422'}],'id': '331799115'},{'answers': [{'choice_id': '2194241445'}],'id': '331799117'},{'answers': [{'choice_id': '2194241457'}],'id': '331799122'},{'answers': [{'choice_id': '2194241466'}],'id': '331799126'},{'answers': [{'choice_id': '2194241500'}],'id': '331799128'},{'answers': [{'choice_id': '2194241535'}],'id': '331799130'},{'answers': [{'choice_id': '2194241555'}],'id': '331799132'}

如何用choice_id字典的值替换answer_id数字?

所有answer_id号都是唯一的。

wdd16617723 回答:如何匹配和修改字典数组?

我认为您可以执行以下操作:

# responseL - The array in the first snippet
# answerL - The array in the second snippet
# Convert responseL to a dictionary
responseD = { k: v for resp_typeD in responseL for k,v in resp_typeD.items() }

for answerD in answerL:
    for questionD in answerD['answers']:
        questionD['choice_id'] = responseD.get(questionD['choice_id'],"UNKNOWN")
本文链接:https://www.f2er.com/3141520.html

大家都在问