从字典键中提取值
问题描述:
你好,
如何从字典中提取键值?
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[^]