回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有下面提到的元组列表,元组中的第一个元素是键,我想根据它生成特定格式的字典。你知道吗</p>
<pre class="lang-py prettyprint-override"><code># list variable
x = [
(('Fresh Vegetables', 'Hamel', '2006', "Jimmy's Ebony Sweet Peas"), 209034.60000000001),
(('Fresh Vegetables', 'Hamel', '2006', "Sue's Ebony Asparagus"), 217158.67000000001),
(('Fresh Vegetables', 'Mentor', '2007', "Jimmy's Ebony Sweet Peas"), 225630.60000000001),
(('Fresh Vegetables', 'Mentor', '2007',"Sue's Ebony Asparagus"), 233201.47),
(('Fresh Vegetables', 'Hamel', '2006', "Bubba's Ebony Corn on the Cob"), 317588.95000000001),
(('Fresh Vegetables','Mentor', '2007', "Bubba's Ebony Corn on the Cob"), 343151.59000000003),
(('Fresh Vegetables', 'Sioux Center', '2007', "John's Ebony Squash"), 401308.01 000000001)
]
</code></pre>
<p>我想要下面给出的词典结构。其中“<strong>total</strong>”键是该字典中每个字典元素的总和。你知道吗</p>
<pre class="lang-py prettyprint-override"><code>finalX = {
'Fresh Vegetables':{
'Hamel':{
'2006':{
"Jimmy's Ebony Sweet Peas":209034.60000000001,
"Sue's Ebony Asparagus":217158.67000000001,
"Bubba's Ebony Corn on the Cob":317588.95000000001,
"total": 743782.22
}
"total":743782.22
},
'Mentor':{
'2007':{
"Jimmy's Ebony Sweet Peas":225630.60000000001,
"Sue's Ebony Asparagus":233201.47,
"Bubba's Ebony Corn on the Cob":343151.59000000003,
"total": 801983.66
}
"total":801983.66
},
'Sioux Center':{
'2007':{
"John's Ebony Squash":401308.01000000001
"total":401308.01
}
"total":401308.01
}
"total":1947073.89
}
}
</code></pre>
<p>我知道没有递归是不可能的,但我被困在那里。如何实施?我试过一些事,但做不到。你知道吗</p>