回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有一本字典,里面有如下的字典清单。你知道吗</p>
<p>我想根据名称交换字典列表的所有值。你知道吗</p>
<p>示例:<code>swap_function('Arvind','Jayesh')</code>应交换其他值,如姓氏、全名和电子邮件。你知道吗</p>
<p>我已经尝试了很多从其他网站的参考,但不能达到我的目标。你知道吗</p>
<pre><code>data = {
"items":[
{
"name":"Arvind",
"surname":"Patel",
"fullname":"Arvind Patel",
"email":"abc@xyx.com"
},
{
"name":"Jayesh",
"surname":"Patel",
"fullname":"Jayesh Patel",
"email":"Patel@gmail.com"
},
{
"name":"Krishna",
"surname":"dave",
"fullname":"Krishna dave",
"email":"Krishna@xyz.com"
},
{
"name":"Aditya",
"surname":"Patel",
"fullname":"Aditya Patel",
"email":"Aditya@abc.com"
}
]
}
</code></pre>
<p>我试过下面这样,但之后我就没有主意了。你知道吗</p>
<pre><code>def name_swap(name1, name2):
for key, item in data.items():
first_dict = item[0]
second_dict = item[1]
third_dict = item[2]
forth_dict = item[3]
fifth_dict = item[4]
</code></pre>
<pre><code>after name_swap('Arvind', 'Krishna')
output :
data = {
"items":[
{
"name":"Arvind",
"surname":"dave",
"fullname":"Krishna dave",
"email":"Krishna@xyz.com"
},
{
"name":"Jayesh",
"surname":"Patel",
"fullname":"Jayesh Patel",
"email":"Patel@gmail.com"
},
{
"name":"Krishna",
"surname":"Patel",
"fullname":"Arvind Patel",
"email":"abc@xyx.com"
},
{
"name":"Aditya",
"surname":"Patel",
"fullname":"Aditya Patel",
"email":"Aditya@abc.com"
}
]
}
</code></pre>