擅长:python、mysql、java
<p>不幸的是,不能把thay放在一个大的听写式中,因为字典中不能有重复的键(而且会有多个“FirstParam”和“SecondParam”键)。如果要创建一个迷你字典列表(每个小字典的键为“FirstParam”和“SecondParam”),则:</p>
<pre><code>iList = [('FirstParam', 1), ('FirstParam', 2), ('FirstParam', 3), ('FirstParam', 4), ('SecondParam', 5), ('SecondParam', 6), ('SecondParam', 7)]
first_params = [] #list for collecting all first params
second_params = [] #list for collecting all second params
#Assuming tuple is all first params and then all second params, can only loop through once (more efficient)
i = 0;
while(i < len(iList) and iList[i][0] == 'FirstParam'):
first_params.append(iList[i])
i+= 1
while(i < len(iList) and iList[i][0] == 'SecondParam'):
second_params.append(iList[i])
i+= 1
#Add to the final list of dictionaries
listOfParamDictionaries = []
for i in range(0, min(len(first_params), len(second_params))):
listOfParamDictionaries.append({first_params[i][0] : first_params[i][1], second_params[i][0] : second_params[i][1]})
</code></pre>