在 foreach 循环中编辑字典值
我正在尝试从字典构建饼图。 在显示饼图之前,我想整理一下数据。 我将删除所有小于饼图 5% 的饼图切片,并将它们放入“其他”饼图切片中。 但是我收到一个 Collection was generated; 枚举操作在运行时可能会出现无法执行的异常。
我理解为什么在迭代字典时无法添加或删除字典中的项目。 但是我不明白为什么你不能简单地更改 foreach 循环中现有键的值。
任何关于修复我的代码的建议,将不胜感激。
Dictionary<string, int> colStates = new Dictionary<string,int>();
// ...
// Some code to populate colStates dictionary
// ...
int OtherCount = 0;
foreach(string key in colStates.Keys)
{
double Percent = colStates[key] / TotalCount;
if (Percent < 0.05)
{
OtherCount += colStates[key];
colStates[key] = 0;
}
}
colStates.Add("Other", OtherCount);
I am trying to build a pie chart from a dictionary. Before I display the pie chart, I want to tidy up the data. I'm removing any pie slices that would be less than 5% of the pie and putting them in a "Other" pie slice. However I'm getting a Collection was modified; enumeration operation may not execute
exception at runtime.
I understand why you can not add or remove items from a dictionary while iterating over them. However I don't understand why you can't simply change a value for an existing key within the foreach loop.
Any suggestions re: fixing my code, would be appreciated.
Dictionary<string, int> colStates = new Dictionary<string,int>();
// ...
// Some code to populate colStates dictionary
// ...
int OtherCount = 0;
foreach(string key in colStates.Keys)
{
double Percent = colStates[key] / TotalCount;
if (Percent < 0.05)
{
OtherCount += colStates[key];
colStates[key] = 0;
}
}
colStates.Add("Other", OtherCount);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
除了其他答案之外,我想我会注意到,如果您获得.KeyCollection 或
sortedDictionary.Keys
或sortedDictionary.Values
,然后使用foreach
循环它们>,您也按排序顺序浏览。 这是因为这些方法返回 System.Collections.Generic.SortedDictionarySortedDictionary.ValueCollection
对象,它们维护原来的字典。Along with the other answers, I thought I'd note that if you get
sortedDictionary.Keys
orsortedDictionary.Values
and then loop over them withforeach
, you also go through in sorted order. This is because those methods returnSystem.Collections.Generic.SortedDictionary<TKey,TValue>.KeyCollection
orSortedDictionary<TKey,TValue>.ValueCollection
objects, which maintain the sort of the original dictionary.该答案用于比较两种解决方案,而不是建议的解决方案。
您可以使用
for
循环,使用字典Count
作为循环停止条件,并使用Keys.ElementAt(i)<,而不是像其他答案建议的那样创建另一个列表。 /code> 获取密钥。
起初我认为这会更有效,因为我们不需要创建密钥列表。 运行测试后,我发现
for
循环解决方案的效率低得多。 原因是因为ElementAt
在dictionary.Keys
属性上的复杂度为 O(n),它从集合的开头搜索,直到找到第 n 个项目。检测
结果:
This answer is for comparing two solutions, not a suggested solution.
Instead of creating another list as other answers suggested, you can used a
for
loop using the dictionaryCount
for the loop stop condition andKeys.ElementAt(i)
to get the key.At firs I thought this would be more efficient because we do not need to create a key list. After running a test I found that the
for
loop solution is much less efficient. The reason is becauseElementAt
is O(n) on thedictionary.Keys
property, it searches from the beginning of the collection until it gets to the nth item.Test:
Results:
让我添加我自己的方法。 我正在使用 Unity 并且肯定使用 Linq。
此代码将冷却时间值减一并清除任何为零的冷却时间,重建原始的
Dictionary
Let me add my own approach to this. I'm using Unity and definitely using Linq.
This code decrements the cooldown values by one and cleans any cooldowns that are zero, rebuilding the original
Dictionary<string, int>
在 .NET 5(可能仍然包括 .NET Framework)之前,在字典中设置值会更新其内部“版本号”,这会使迭代器以及与键或值集合关联的任何迭代器无效。
我确实明白你的观点,但同时如果值集合可以在迭代中改变,那就很奇怪了 - 为了简单起见,只有一个版本号。
修复此类问题的正常方法是预先复制键集合并迭代副本,或者迭代原始集合但维护一个更改集合,您将在完成迭代后应用这些更改。
例如:
首先复制密钥
或者...
创建修改列表
Prior to .NET 5 (and presumably still including .NET Framework), setting a value in a dictionary updates its internal "version number" - which invalidates the iterator, and any iterator associated with the keys or values collection.
I do see your point, but at the same time it would be odd if the values collection could change mid-iteration - and for simplicity there's only one version number.
The normal way of fixing this sort of thing is to either copy the collection of keys beforehand and iterate over the copy, or iterate over the original collection but maintain a collection of changes which you'll apply after you've finished iterating.
For example:
Copying keys first
Or...
Creating a list of modifications
在
foreach
循环中调用ToList()
。 这样我们就不需要临时变量副本。 它依赖于从 .Net 3.5 起可用的 Linq。Call the
ToList()
in theforeach
loop. This way we dont need a temp variable copy. It depends on Linq which is available since .Net 3.5.您正在修改此行中的集合:
通过这样做,您实际上是在此时删除并重新插入某些内容(无论如何,就 IEnumerable 而言)。
如果您编辑要存储的值的成员,那就没问题,但是您编辑值本身,而 IEnumberable 不喜欢这样。
我使用的解决方案是消除 foreach 循环,只使用 for 循环。
简单的 for 循环不会检查您知道不会影响集合的更改。
您可以这样做:
You are modifying the collection in this line:
By doing so, you are essentially deleting and reinserting something at that point (as far as IEnumerable is concerned anyways.
If you edit a member of the value you are storing, that would be OK, but you are editing the value itself and IEnumberable doesn't like that.
The solution I've used is to eliminate the foreach loop and just use a for loop.
A simple for loop won't check for changes that you know won't effect the collection.
Here's how you could do it:
在 .NET 5 中,枚举字典时可以更改字典项。
拉取请求是:允许在枚举期间覆盖字典,问题是考虑从 Dictionary 中的覆盖中删除 _version++。
现在你可以:
In .NET 5 the dictionary items can be changed while the dictionary is enumerated.
The Pull Request is: Allow Dictionary overwrites during enumeration and the issue is Consider removing _version++ from overwrites in Dictionary<TKey, TValue>.
Now you can:
您不能直接在 ForEach 中修改键或值,但可以修改它们的成员。 例如,这应该有效:
You can't modify the keys nor the values directly in a ForEach, but you can modify their members. E.g., this should work:
只对你的字典进行一些 linq 查询,然后将你的图表绑定到这些查询的结果怎么样?...
How about just doing some linq queries against your dictionary, and then binding your graph to the results of those?...
如果你觉得有创意,你可以做这样的事情。 向后循环字典以进行更改。
当然不完全相同,但无论如何你可能会感兴趣......
If you're feeling creative you could do something like this. Loop backwards through the dictionary to make your changes.
Certainly not identical, but you might be interested anyways...
您需要从旧词典创建新词典,而不是就地修改。 类似的东西(也迭代 KeyValuePair<,> 而不是使用键查找:
You need to create a new Dictionary from the old rather than modifying in place. Somethine like (also iterate over the KeyValuePair<,> rather than using a key lookup:
从 .NET 4.5 开始,您可以使用 ConcurrentDictionary:
但请注意,它的性能实际上比简单的
foreachdictionary.Kes.ToArray()
差得多:结果:
Starting with .NET 4.5 You can do this with ConcurrentDictionary:
Note however that its performance is actually much worse that a simple
foreach dictionary.Kes.ToArray()
:Result:
您无法修改集合,甚至不能修改值。 您可以保存这些案例并稍后将其删除。 它最终会是这样的:
You can't modify the collection, not even the values. You could save these cases and remove them later. It would end up like this:
免责声明:我不做太多 C#
您正在尝试修改存储在 HashTable 中的 DictionaryEntry 对象。 Hashtable 只存储一个对象——DictionaryEntry 的实例。 更改 Key 或 Value 足以更改 HashTable 并导致枚举器变得无效。
您可以在循环之外执行此操作:
首先创建您希望更改的值的所有键的列表,然后迭代该列表。
Disclaimer: I don't do much C#
You are trying to modify the DictionaryEntry object which is stored in the HashTable. The Hashtable only stores one object -- your instance of DictionaryEntry. Changing the Key or the Value is enough to change the HashTable and cause the enumerator to become invalid.
You can do it outside of the loop:
by first creating a list of all the keys of the values you wish to change and iterate through that list instead.
您可以制作
dict.Values
的列表副本,然后可以使用List.ForEach
lambda 函数进行迭代(或foreach
循环,如之前建议的那样)。You can make a list copy of the
dict.Values
, then you can use theList.ForEach
lambda function for iteration, (or aforeach
loop, as suggested before).