从字典键中提取值 [英] extract value from dictionary key

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

问题描述

你好,
如何从字典中提取键值?

Hello,
How do you extract the key value from the dictionary?

private Dictionary<string, double> g_masses = new Dictionary<string, double>();

          private void PopulateWeightDictionary()
          {
               g_masses.Add("K", 3);
               g_masses.Add("R", 1);
               g_masses.Add("S", 5);
               g_masses.Add("L", 22);
               g_masses.Add("G", 1);


}


}

推荐答案

我个人将对字符串进行排序并只计算重复次数:
Personally, I would sort the string and just count the repetitions:
string s = "KRRSLSG";
char[] data = s.ToCharArray();
Array.Sort(data);
char last = '\0';
Dictionary<char, int> counts = new Dictionary<char, int>();
foreach (char c in data)
    {
    if (last == c)
        {
        counts[c]++;
        }
    else
        {
        last = c;
        counts.Add(c, 1);
        }
    }


尝试
http://www.dotnetperls.com/count-characters [ http://www.dotnetperls.com/string-occurrence [
Try
http://www.dotnetperls.com/count-characters[^]
http://www.dotnetperls.com/string-occurrence[^]


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

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