【问题标题】:Loop through multiple dictionaries循环遍历多个字典
【发布时间】:2016-11-04 11:37:20
【问题描述】:

我有 6 个字典。我想将另一个字典与它们中的每一个进行比较,看看哪些字典包含哪些字符串。是否可以使用 foreach 循环?

static Dictionary<string, int> d = new Dictionary<string, int>();
static Dictionary<string, double> dNL = new Dictionary<string, double>();
static Dictionary<string, double> dDE = new Dictionary<string, double>();
static Dictionary<string, double> dFR = new Dictionary<string, double>();
static Dictionary<string, double> dSP = new Dictionary<string, double>();
static Dictionary<string, double> dEN = new Dictionary<string, double>();
static Dictionary<string, double> dIT = new Dictionary<string, double>();

foreach (var f in d)
{
    if (dNL.ContainsKey(f.Key))
    {
        //add to a numeric?
    }
    if (dDE.ContainsKey(f.Key))
    {
        //add to a numeric?
    }
}

这样的?

我目前拥有的(并且没有按预期工作):

// need to find a better solution
foreach (var f in d)
{
    if (dNL.ContainsKey(f.Key))
    {
        dNLtotaal++;
    }
}
foreach (var f in d)
{
    if (dDE.ContainsKey(f.Key))
    {
        dDEtotaal++;
    }
}
foreach (var f in d)
{
    if (dFR.ContainsKey(f.Key))
    {
        dFRtotaal++;
    }
}
foreach (var f in d)
{
    if (dSP.ContainsKey(f.Key))
    {
        dSPtotaal++;
    }
}
foreach (var f in d)
{
    if (dEN.ContainsKey(f.Key))
    {
        dENtotaal++;
    }
}
foreach (var f in d)
{
    if (dIT.ContainsKey(f.Key))
    {
        dITtotaal++;
    }
}
// NEED A MUCH BETTER SOLUTION
List<int> totaleD = new List<int>();
totaleD.Add(dNLtotaal);
totaleD.Add(dDEtotaal);
totaleD.Add(dFRtotaal);
totaleD.Add(dSPtotaal);
totaleD.Add(dENtotaal);
totaleD.Add(dITtotaal);
int max = !totaleD.Any() ? -1 : totaleD.Select((value, index) => new { Value = value, Index = index }).Aggregate((a, b) => (a.Value > b.Value) ? a : b).Index;
var maxIndex = totaleD.IndexOf(totaleD.Max());
Console.WriteLine(maxIndex);

【问题讨论】:

  • 你可以用 lambda 缩短它,我现在不能举个例子,对不起
  • 你能解释一下你的最后一行应该做什么吗?它应该告诉你哪个字典匹配最多?
  • 您的代码究竟是如何没有按预期工作的?
  • 您可以将所有这些 foreach 语句组合成一个其中只有每个 if 语句的语句,并且您可以循环遍历 dKeys
  • @Rob 是的,我正在尝试获取与其匹配最多的字典,但我与一位朋友交谈,我发现了这个问题的问题。我正在计算字典中有多少键,但每个字典都有 1 或 0 个包含该二元组的键。例如;他在每个字典中只出现 1 次,所以每个 int 的值都是 1。

标签: c# dictionary foreach


【解决方案1】:

你可以这样做:

var items = d.Keys;
var dictionaries = new[] { dNL, dDE, dFR, dSP, dEN, dIT };
var result = dictionaries.Select((d, index) =>
    new {
        Index = index,
        Matches = items.Count(i => d.ContainsKey(i))
    })
    .OrderByDescending(i => i.Matches)
    .Select(i => i.Index)
    .FirstOrDefault();

哪个为您提供匹配最多的字典索引

【讨论】:

  • 我假设items 应该是d.Keys
  • @juharr 是的 - 抱歉,我错过了 d 的声明,并认为它是 List&lt;string&gt;。谢谢你的收获
【解决方案2】:

您可以使用 lambda 表达式来获得所需的结果。在以下示例中,我尝试使用两个字典:

int dNLtotaal = 0;
Dictionary<string, double> dNL = new Dictionary<string, double>();
Dictionary<string, double> dDE = new Dictionary<string, double>();

dNL.Keys.Where(k => dDE.ContainsKey(k)).ToList().ForEach(k => dNLtotaal++);

希望对你有帮助

【讨论】:

  • 不使用ToListForeach,您可以使用Count 并将结果分配给dNLtotal
【解决方案3】:

为什么不用 1 个字典而不是 6 个?并保留一对 [string, List[SomeObject]] 其中 SomeObject 是一个类似的类

class SomeObject
{
    public Enum Type;//dNL, dDE etc
    public double Value;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-11
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多