如何替换字典中键的值? [英] how to replace the values of a key in a dictionary?

查看:377
本文介绍了如何替换字典中键的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在迭代时替换字典中键的值?

解决方案

点击这里 [ ^ ]


您无需迭代更改字典。

尝试

 dict.Remove(keyToUpdate); 
dict.Add(newKey, value );





在迭代它时,您将无法更改集合。

一种方法是创建一个临时的键值对列表,遍历此列表并根据需要更改密钥。 / blockquote>

请参阅此文章以帮助您, http:/ /stackoverflow.com/questions/6499334/best-way-to-change-dictionary-key [ ^ ],一旦添加到Dictionary中,你就不能重命名键,先删除然后添加一个新的。


how to replace the values of a key in a dictionary while iterating?

解决方案

click here[^]


You don't need to iterate to change the dictionary.
Try

dict.Remove(keyToUpdate);
dict.Add(newKey, value);



You will not be able to change the collection while you are iterating through it.
A way out is to create a temporary list of key value pairs, iterate through this list and change the key as appropriate.


See this post to help you,http://stackoverflow.com/questions/6499334/best-way-to-change-dictionary-key[^], you can't rename keys once that have been added to a Dictionary, first remove then add a new.


这篇关于如何替换字典中键的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆