我有這 3 個串列:
list1(問題):
['Which of the following would best describe the meaning of value in a shippable increment?(choose the best two answers)', 'The Scrum Team consists of: (choose all that apply)']
list2(正確):(串列串列)
[['It is likely to increase customer engagement and happiness with the product.', 'It reduces long-term operational costs.'], ['The Developers', 'The Scrum Master', 'The Product Owner']]
list3(不正確):(串列串列)
[['It has all User Stories that were committed to at the Sprint Planning.', 'It is completed on time.', 'It is approved by the Product Owner at the Sprint Review.'], ['The Key Stakeholders']]
我想創建一個包含這 3 個串列的串列,它應該如下所示:
[{'question': 'Which of the following would best describe the meaning of value in a shippable increment?(choose the best two answers)',
'correct': ['It is likely to increase customer engagement and happiness with the product',
'It reduces long-term operational costs.'],
'incorrect': ['It has all User Stories that were committed to at the Sprint Planning.',
'It is completed on time.]},
{'question': 'The Scrum Team consists of: (choose all that apply)',
'correct': ['The Developers',
'The Scrum Master',
'The Product Owner'],
'incorrect': ['The Key Stakeholders']} ]
因此,對于每個問題,我們都會得到正確答案和錯誤答案,并且還需要添加“問題:”/“正確:”/“不正確”。如果有人可以幫助我請
uj5u.com熱心網友回復:
我想說最好的解決方案是:
lol = [{'question': x[0], 'correct': x[1], 'incorrect': x[2]} for x in zip(list1, list2, list3)]
這個想法是zip()串列,它是一個函式,它需要幾個 (1 ) 可迭代/容器并將它們連接到一個串列中。例如:
In [1]: list(zip([1, 2], [3, 4]))
Out[1]: [(1, 3), (2, 4)]
從這里開始,我們只需遍歷壓縮串列,并從每個元組中創建一個 dict。
uj5u.com熱心網友回復:
嘗試使用這種方法多次使用zip. 這將通過以下方式作業(請參閱圖表以獲得視覺直覺) -

- 首先,壓縮 3 個串列中每個對應的元素。這將產生一個包含 2 個專案的串列,其中每個專案是一個包含 3 個元素(Q、C、I)的元組
- 其次,使用上述 zip 物件的每個元素壓縮鍵(包含鍵名/變數名)。這將導致
(Q0, C0, I0)zipped with["question", "correct", "incorrect"]看起來[("question",Q0), ("correct":C0), ("incorrect":I0)]和(Q1, C1, I1). - 最后
dict()將其轉換為您正在尋找的鍵值對。
question = ['Which of the following would best describe the meaning of value in a shippable increment?(choose the best two answers)', 'The Scrum Team consists of: (choose all that apply)']
correct = [['It is likely to increase customer engagement and happiness with the product.', 'It reduces long-term operational costs.'], ['The Developers', 'The Scrum Master', 'The Product Owner']]
incorrect = [['It has all User Stories that were committed to at the Sprint Planning.', 'It is completed on time.', 'It is approved by the Product Owner at the Sprint Review.'], ['The Key Stakeholders']]
keys = ["question", "correct", "incorrect"]
output = [dict(zip(keys,i)) for i in zip(question, correct, incorrect)] #<-----
print("First dictionary")
print(output[0])
print("Second dictionary")
print(output[1])
First dictionary
{'question': 'Which of the following would best describe the meaning of value in a shippable increment?(choose the best two answers)',
'correct': ['It is likely to increase customer engagement and happiness with the product.', 'It reduces long-term operational costs.'],
'incorrect': ['It has all User Stories that were committed to at the Sprint Planning.', 'It is completed on time.', 'It is approved by the Product Owner at the Sprint Review.']}
Second dictionary
{'question': 'The Scrum Team consists of: (choose all that apply)',
'correct': ['The Developers', 'The Scrum Master', 'The Product Owner'],
'incorrect': ['The Key Stakeholders']}
uj5u.com熱心網友回復:
您可以通過將 Python 串列嵌套到另一個串列中來嵌套它們。
例如
nestedList = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', 'h']
您在上面使用的花括號通常定義字典。
如果您仍然想查找串列 - 即使 dict 非常適合 - 您可以選擇這樣的東西:
[['question1 delimiter answer'], ['question2 delimiter answer'], ... ]
或者
[['question1']['delimiter']['answer'], ....]
或者
[[['question1']['delimiter']['answer']], ....]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/477423.html
