特定键的字典的总和值 C#
Sum values of a Dictionary for specific keys C#
我有一个字典,其中有两个相似的键,一个拼写错误,因此它创建了另一个键,我想对它的值求和,然后删除拼写错误的 key/value 对。
如果我有一个部门字典,以部门名称为键,该部门的人员数量为值,我想将 "Marketing" 和 [=21 的值相加=] 并删除拼写错误的 key/value 对 "Marketin"。
var departments = new Dictionary<string, int>()
{
{ "HR", 33 },
{ "Management", 8 },
{ "Marketing", 21 },
{ "Marketin", 4 },
{ "Sales", 44 }
};
我希望字典看起来像这样:
var departments = new Dictionary<string, int>()
{
{ "HR", 33 },
{ "Management", 8 },
{ "Marketing", 25 },
{ "Sales", 44 }
};
字典已经创建,所以我只是为了说明目的使用了初始化。
departments["Marketing"] += departments["Marketin"];
departments.Remove("Marketin");
这是一个代码示例,它说明了您可以用来执行所需操作的词典功能。
至于您需要使用这些功能的哪种组合来实现您的确切目的,我将把它作为练习留给您。如果您需要更多帮助。发表评论,准确解释您不理解的内容。
//how to get a value in your dictionary
string myValue = departments["Marketin"];
//how to update a value in your dictionary
departments["Marketing"] = 5;
//how to remove a value in your dictionary
departments.Remove("Marketin");
//how to iterate over your dictionary
foreach(var department in departments)
{
//only print the value if it's "Marketin"
if(department.Key == "Marketin")
{
Console.WriteLine(department.Value);
}
}
I would like to sum the values for "Marketing" and "Marketin" and delete the misspelled key/value pair "Marketin".
这里是用户识别出问题文本和正确文本,然后删除不正确的文本并将值添加(求和)到正确键的值。
var departments = new Dictionary<string, int>()
{
{ "HR", 33 },
{ "Management", 8 },
{ "Marketing", 21 },
{ "Marketin", 4 },
{ "Sales", 44 }
};
var baseText = "Market";
var validText = "Marketing";
var removal = departments.ToList()
.Where(itm => itm.Key.StartsWith(baseText) &&
itm.Key.Equals(validText) == false)
.ToList();
removal.ForEach(itm => departments.Remove(itm.Key));
departments[validText] += removal.Sum(itm => itm.Value);
最终结果:
这是我对更多 "generic" 方法的草图:
var normalisedDepartments =
departments.GroupBy(x => Normalise(x.Key), y => y.Value)
.ToDictionary(x => x.Key, y => y.Sum());
private static string Normalise(string key)
{
if (key == "Marketin")
{
return "Marketing";
}
return key;
}
然后你可以调整 Normalize 方法,以便它识别其他格式错误的键,如果有更多的话。
我有一个字典,其中有两个相似的键,一个拼写错误,因此它创建了另一个键,我想对它的值求和,然后删除拼写错误的 key/value 对。
如果我有一个部门字典,以部门名称为键,该部门的人员数量为值,我想将 "Marketing" 和 [=21 的值相加=] 并删除拼写错误的 key/value 对 "Marketin"。
var departments = new Dictionary<string, int>()
{
{ "HR", 33 },
{ "Management", 8 },
{ "Marketing", 21 },
{ "Marketin", 4 },
{ "Sales", 44 }
};
我希望字典看起来像这样:
var departments = new Dictionary<string, int>()
{
{ "HR", 33 },
{ "Management", 8 },
{ "Marketing", 25 },
{ "Sales", 44 }
};
字典已经创建,所以我只是为了说明目的使用了初始化。
departments["Marketing"] += departments["Marketin"];
departments.Remove("Marketin");
这是一个代码示例,它说明了您可以用来执行所需操作的词典功能。
至于您需要使用这些功能的哪种组合来实现您的确切目的,我将把它作为练习留给您。如果您需要更多帮助。发表评论,准确解释您不理解的内容。
//how to get a value in your dictionary
string myValue = departments["Marketin"];
//how to update a value in your dictionary
departments["Marketing"] = 5;
//how to remove a value in your dictionary
departments.Remove("Marketin");
//how to iterate over your dictionary
foreach(var department in departments)
{
//only print the value if it's "Marketin"
if(department.Key == "Marketin")
{
Console.WriteLine(department.Value);
}
}
I would like to sum the values for "Marketing" and "Marketin" and delete the misspelled key/value pair "Marketin".
这里是用户识别出问题文本和正确文本,然后删除不正确的文本并将值添加(求和)到正确键的值。
var departments = new Dictionary<string, int>()
{
{ "HR", 33 },
{ "Management", 8 },
{ "Marketing", 21 },
{ "Marketin", 4 },
{ "Sales", 44 }
};
var baseText = "Market";
var validText = "Marketing";
var removal = departments.ToList()
.Where(itm => itm.Key.StartsWith(baseText) &&
itm.Key.Equals(validText) == false)
.ToList();
removal.ForEach(itm => departments.Remove(itm.Key));
departments[validText] += removal.Sum(itm => itm.Value);
最终结果:
这是我对更多 "generic" 方法的草图:
var normalisedDepartments =
departments.GroupBy(x => Normalise(x.Key), y => y.Value)
.ToDictionary(x => x.Key, y => y.Sum());
private static string Normalise(string key)
{
if (key == "Marketin")
{
return "Marketing";
}
return key;
}
然后你可以调整 Normalize 方法,以便它识别其他格式错误的键,如果有更多的话。