简体   繁体   中英

How to find a key from a list of dictionary?

I have a list like this.

MCQ = [[{'answer': [{'answer': 'Indian', 'correct': False},
    {'answer': 'Brahmanism', 'correct': False},
    {'answer': 'Hindu', 'correct': False},
    {'answer': 'Vedic Brahmanism', 'correct': True}],
   'question': 'What religions were synthesised with the preexisting cultures of the subcontinent?'},
  {'answer': [{'answer': 'the Maurya Empire', 'correct': True},
    {'answer': 'the Gupta Empire', 'correct': False},
    {'answer': 'Punjab', 'correct': False},
    {'answer': 'Central Asia', 'correct': False}],
   'question': 'What Empire ruled the Indian subcontinent?'},
  {'answer': [{'answer': 'the Indus Valley Civilization', 'correct': True},
    {'answer': 'BCE', 'correct': False},
    {'answer': 'Wootz', 'correct': False},
    {'answer': 'Prakrit', 'correct': False}],
   'question': 'What was the first civilization of the Old world?'},
  {'answer': [{'answer': 'Southeast Asia', 'correct': False},
    {'answer': 'Vedic', 'correct': True},
    {'answer': 'Asia', 'correct': False},
    {'answer': 'the Indus Valley', 'correct': False}],
   'question': 'What religions were associated with the urbanisation of Greater Magadha?'},
  {'answer': [{'answer': 'the next 1500 years', 'correct': False},
    {'answer': 'between 73000 and 55000 years ago', 'correct': False},
    {'answer': 'the second millennium', 'correct': True},
    {'answer': 'the 3rd century', 'correct': False}],
   'question': 'When did the Indus Valley begin to disappear?'}]]

I want to differentiate a list like this

output = [{'question': 'What religions were synthesised with the preexisting cultures of the subcontinent?','options':['Indian','Brahmanism','Hindu','Vedic Brahmanism'],'answer':'Vedic Brahmanism'},{'question': 'What Empire ruled the Indian subcontinent?','options':['the Maurya Empire','the Gupta Empire','Punjab','Central Asia'],'answer':'the Maurya Empire'}]

Note 1: options field (in output) contain all the answer field in MCQ.

EX. [{'answer': [{'answer': 'Indian', 'correct': False},
{'answer': 'Brahmanism', 'correct': False},
{'answer': 'Hindu', 'correct': False},
{'answer': 'Vedic Brahmanism', 'correct': True}] (from MCQ)
**options** = Indian , Brahmanism , Hindu , Vedic Brahmanism (in output)

Note 2: answer field (in output) contain True value in answer -> answer -> 'correct': True (in MCQ)

EX. [{'answer': [{'answer': 'Indian', 'correct': False},
    {'answer': 'Brahmanism', 'correct': False},
    {'answer': 'Hindu', 'correct': False},
    {'answer': 'Vedic Brahmanism', 'correct': True}] (from MCQ)
    answer = Vedic Brahmanism (in output)

One solution:

output = []
for e in MCQ:
    for d in e:
        row = {"options": []}
        for a in d["answer"]:
            row["options"].append(a["answer"])
            if a["correct"]:
                row["answer"] = a["answer"]
        row["question"] = d["question"]
        output.append(row)
print(output)

Output

[{'answer': 'Vedic Brahmanism',
  'options': ['Indian', 'Brahmanism', 'Hindu', 'Vedic Brahmanism'],
  'question': 'What religions were synthesised with the preexisting cultures of the subcontinent?'},
 {'answer': 'the Maurya Empire',
  'options': ['the Maurya Empire', 'the Gupta Empire', 'Punjab', 'Central Asia'],
  'question': 'What Empire ruled the Indian subcontinent?'},
 {'answer': 'the Indus Valley Civilization',
  'options': ['the Indus Valley Civilization', 'BCE', 'Wootz', 'Prakrit'],
  'question': 'What was the first civilization of the Old world?'},
 {'answer': 'Vedic',
  'options': ['Southeast Asia', 'Vedic', 'Asia', 'the Indus Valley'],
  'question': 'What religions were associated with the urbanisation of Greater Magadha?'},
 {'answer': 'the second millennium',
  'options': ['the next 1500 years', 'between 73000 and 55000 years ago', 'the second millennium', 'the 3rd century'],
  'question': 'When did the Indus Valley begin to disappear?'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM