在fo​​reach循环编辑字典值 [英] Editing dictionary values in a foreach loop

查看:254
本文介绍了在fo​​reach循环编辑字典值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立从字典饼图。在我显示饼图,我想整理一下数据。我删除任何扇形,将是馅饼的不到5%,并把它们放在其他饼片。但是我得到一个集合已修改;在运行时枚举操作可能不会执行例外。

我明白你为什么不能添加或从字典在遍历他们删除项目。不过,我不明白为什么你不能简单地改变一个值的foreach循环内现有的密钥。

任何建议重新:固定我的code,将AP preciated

 词典<字符串,INT> colStates =新字典<字符串,INT>();
// ...
//有些code填充colStates字典
// ...

INT OtherCount = 0;

的foreach(在colStates.Keys字符串键)
{

    双百分比= colStates [关键] / TotalCount;

    如果(百分比< 0.05)
    {
        OtherCount + = colStates [关键];
        colStates [关键] = 0;
    }
}

colStates.Add(其他,OtherCount);
 

解决方案

在字典中设置一个值来更新其内部的版本号 - 其中迭代无效,并且与键或值集合相关联的任何迭代

我不明白你的意思,但在同一时间,它会奇怪,如果值集合可以改变中期迭代 - 为简单起见,只有一个版本号

固定这样的事情的方式,一般是可以复制键的收集事前遍历复制或遍历原来的集合,但保持变化的集合你完成迭代后,您将应用

例如:

复制键第一个

 名单,其中,串>键=新的名单,其中,串>(colStates.Keys);
的foreach(在密钥字符串键)
{
    双百分号= colStates [关键] / TotalCount;
    如果(百分号< 0.05)
    {
        OtherCount + = colStates [关键];
        colStates [关键] = 0;
    }
}
 

或者

创建修改列表

 名单,其中,串> keysToNuke =新的名单,其中,串>();
的foreach(在colStates.Keys字符串键)
{
    双百分号= colStates [关键] / TotalCount;
    如果(百分号< 0.05)
    {
        OtherCount + = colStates [关键];
        keysToNuke.Add(键);
    }
}
的foreach(在keysToNuke字符串键)
{
    colStates [关键] = 0;
}
 

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);

解决方案

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

List<string> keys = new List<string>(colStates.Keys);
foreach(string key in keys)
{
    double percent = colStates[key] / TotalCount;    
    if (percent < 0.05)
    {
        OtherCount += colStates[key];
        colStates[key] = 0;
    }
}

Or...

Creating a list of modifications

List<string> keysToNuke = new List<string>();
foreach(string key in colStates.Keys)
{
    double percent = colStates[key] / TotalCount;    
    if (percent < 0.05)
    {
        OtherCount += colStates[key];
        keysToNuke.Add(key);
    }
}
foreach (string key in keysToNuke)
{
    colStates[key] = 0;
}

这篇关于在fo​​reach循环编辑字典值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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