回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>给定的是输入列表,其中包含字典</p>
<pre><code>Input=[
{"rbd":"x","cat":"xxx","origin":"us"},
{"rbd":"y","cat":"xxx","origin":"us"},
{"rbd":"z","cat":"xxx","origin":"us"},
{"rbd":"q","cat":"xxx","origin":"us"},
{"rbd":"1","cat":"xxy","origin":"us"},
{"rbd":"2","cat":"xxy","origin":"us"}
]
</code></pre>
<p>我试图把它作为一个输出。
这里,如果输入列表中的字典的键“cat”和键“origin”相同,那么它会将它们添加到一个“rbd”中。否则它将创建一个新的。下面是输出</p>
<pre><code>Output=[{"rbd":["x","y","z","q"],"cat":"xxx","origin":"us"},
{"rbd":["l","2"],"cat":"xxy","origin":"us"}]
</code></pre>
<p>我尝试了以下不完整的实现。首先将列表转换为字典。它抛出了一个错误</p>
<pre><code>def Convert(lst):
Input_Dict = {lst[i]: lst[i + 1] for i in range(0, len(lst), 2)}
return Input_Dict
</code></pre>