IndexError:将列表转换为字典时,列表索引超出范围


input_list=[[('This civilisation flourished between 2500 __________________ and 1900 __________________ in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.',
'This civilisation flourished between 2500 BCE and 1900 BCE in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.'),
'BCE'],
[('Around the same time IndoAryan tribes moved into the Punjab from __________________ Asia in several waves of migration.',
'Around the same time IndoAryan tribes moved into the Punjab from Central Asia in several waves of migration.'),
'Central']]

output= [{'question': 'This civilisation flourished between 2500 __________________ and 1900 __________________ in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.',
'context':'This civilisation flourished between 2500 BCE and 1900 BCE in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.',
'answer': 'BCE'},
{'question': 'Around the same time IndoAryan tribes moved into the Punjab from __________________ Asia in several waves of migration.',
'context': 'Around the same time IndoAryan tribes moved into the Punjab from Central Asia in several waves of migration.',
'answer': 'Central'}]

我试过上面的方法,但它给了我一个索引错误。我认为这是由于圆括号造成的。

generated_answer=[{'question': l[0], 'context':l[1],'answer': l[2]} for l in input_list]

那么我怎样才能得到如图所示的输出呢?

您的数据具有如下模式[[('question','context'),'answer'],...]

试试这个

input_list=[[('This civilisation flourished between 2500 __________________ and 1900 __________________ in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.',
'This civilisation flourished between 2500 BCE and 1900 BCE in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.'),
'BCE'],
[('Around the same time IndoAryan tribes moved into the Punjab from __________________ Asia in several waves of migration.',
'Around the same time IndoAryan tribes moved into the Punjab from Central Asia in several waves of migration.'),
'Central']]
generated_answer=[{'question': l[0][0], 'context':l[0][1],'answer': l[1]} for l in input_list]
print(generated_answer)

输出

[{'question': 'This civilisation flourished between 2500 __________________ and 1900 __________________ in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.',
'context': 'This civilisation flourished between 2500 BCE and 1900 BCE in what today is Pakistan and northwestern India and was noted for its urban planning baked brick houses elaborate drainage and water supply.',
'answer': 'BCE'}, 
{'question': 'Around the same time IndoAryan tribes moved into the Punjab from __________________ Asia in several waves of migration.',
'context': 'Around the same time IndoAryan 
tribes moved into the Punjab from Central Asia in several waves of migration.', 
'answer': 'Central'}]

你可以这样做:

output = [{'question': q, 'context': c, 'answer': a} for (q, c), a in input_list]

最新更新