這個問題在這里已經有了答案: 將 python 串列轉換為字典 3 個答案 3 小時前關閉。
我有一個這樣的清單。包含問題及其答案。
list=[['It has been noted that in __________________ urban cities quickly expanded rather than the slow evolution of urbanism in the northwest.',
'Gujarat'],
['These traditions and cultures include Anarta Tradition c. 39501900 BC Padri Ware 36002000 BC PrePrabhas Assemblage 32002600 BC Pre Urban Harappan Sindh __________________ Pottery Burial Pottery 30002600 BC Black and Red Ware 3950900 BC Reserved Slip Ware 39501900 BC Micaceous Red Ware 26001600 BC.',
'Type'],
['Later in the 18th century Gujarat came under control of the __________________ Empire who dominated the politics of India.',
'Maratha']]
我想轉換成給定的形式。
output =[{'question': 'It has been noted that in __________________ urban cities quickly expanded rather than the slow evolution of urbanism in the northwest.','answer':'Gujarat'}, {'question': 'These traditions and cultures include Anarta Tradition c. 39501900 BC Padri Ware 36002000 BC PrePrabhas Assemblage 32002600 BC Pre Urban Harappan Sindh __________________ Pottery Burial Pottery 30002600 BC Black and Red Ware 3950900 BC Reserved Slip Ware 39501900 BC Micaceous Red Ware 26001600 BC.','answer':'Type'}, {'question': 'Later in the 18th century Gujarat came under control of the __________________ Empire who dominated the politics of India.','answer':'Maratha'}
可能嗎 ?如果是,那么如何?
uj5u.com熱心網友回復:
串列理解就足夠了:
[{'question': q, 'answer': a} for q, a in list]
這輸出:
[{'question': 'It has been noted that in __________________ urban cities '
'quickly expanded rather than the slow evolution of urbanism in '
'the northwest.',
'answer': 'Gujarat'},
{'question': 'These traditions and cultures include Anarta Tradition c. '
'39501900 BC Padri Ware 36002000 BC PrePrabhas Assemblage '
'32002600 BC Pre Urban Harappan Sindh __________________ Pottery '
'Burial Pottery 30002600 BC Black and Red Ware 3950900 BC '
'Reserved Slip Ware 39501900 BC Micaceous Red Ware 26001600 BC.',
'answer': 'Type'},
{'question': 'Later in the 18th century Gujarat came under control of the '
'__________________ Empire who dominated the politics of India.',
'answer': 'Maratha'}]
uj5u.com熱心網友回復:
你可以試試這個:(考慮不要使用內置函式作為變數。list 是一個內置函式。)
# >>> lst = list
>>> [{'question': l[0], 'answer': l[1]} for l in lst]
[{'question': 'It has been noted that in __________________ urban cities quickly expanded rather than the slow evolution of urbanism in the northwest.',
'answer': 'Gujarat'},
{'question': 'These traditions and cultures include Anarta Tradition c. 39501900 BC Padri Ware 36002000 BC PrePrabhas Assemblage 32002600 BC Pre Urban Harappan Sindh __________________ Pottery Burial Pottery 30002600 BC Black and Red Ware 3950900 BC Reserved Slip Ware 39501900 BC Micaceous Red Ware 26001600 BC.',
'answer': 'Type'},
{'question': 'Later in the 18th century Gujarat came under control of the __________________ Empire who dominated the politics of India.',
'answer': 'Maratha'}]
uj5u.com熱心網友回復:
添加以下行,然后您將獲得所需的輸出:
新串列 = []
對于串列中的資料:
# data[0] contains first element and data[1] contains second element.
dict = {'question': data[0], 'answer': data[1]}
new_list.append(dict)
只需創建一個新串列并附加問題和答案的資料字典。
uj5u.com熱心網友回復:
可以這樣做:
my_list = [['It has been noted that in __________________ urban cities quickly expanded rather than the slow evolution of urbanism in the northwest.',
'Gujarat'],
['These traditions and cultures include Anarta Tradition c. 39501900 BC Padri Ware 36002000 BC PrePrabhas Assemblage 32002600 BC Pre Urban Harappan Sindh __________________ Pottery Burial Pottery 30002600 BC Black and Red Ware 3950900 BC Reserved Slip Ware 39501900 BC Micaceous Red Ware 26001600 BC.',
'Type'],
['Later in the 18th century Gujarat came under control of the __________________ Empire who dominated the politics of India.',
'Maratha']]
output = [{'question': item[0], 'answer': item[1]} for item in my_list]
提示:不要命名與 python 內置資料型別(如串列、集合等)沖突的變數,因為它可能會產生不良后果
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/496476.html
上一篇:如何連接到使用IPv4發現的IPv6鏈路本地地址并設定正確的網路介面?
下一篇:如何將字典值轉換為串列?
