代码之家  ›  专栏  ›  技术社区  ›  Alex Gordon

如何从一本字典中减去另一本?

  •  1
  • Alex Gordon  · 技术社区  · 5 年前

    我们怎样才能减去两本词典,得到有差异的第三本词典呢?

    我有本字典: Dictionary<string, int>() :

    +-------+----+
    | alex  | 10 |
    | liza  | 10 |
    | harry | 20 |
    +-------+----+
    

    从这本字典里,我想 减去 本词典:

    +-------+---+
    | alex  | 5 |
    | liza  | 4 |
    | harry | 1 |
    +-------+---+
    

    我要找的结果是:

    +-------+----+
    | alex  |  5 |
    | liza  |  6 |
    | harry | 19 |
    +-------+----+
    

    我们怎样才能减去两本词典,得到有差异的第三本词典呢?

    1 回复  |  直到 5 年前
        1
  •  5
  •   41686d6564    5 年前

    我们可以假设他们总是有相同的钥匙

    在这种情况下,使用简单的foreach循环非常简单完整示例:

    var dict1 = new Dictionary<string, int>();
    dict1.Add("alex", 10);
    dict1.Add("liza", 10);
    dict1.Add("harry", 20);
    
    var dict2 = new Dictionary<string, int>();
    dict2.Add("alex", 5);
    dict2.Add("liza", 4);
    dict2.Add("harry", 1);
    
    var dict3 = new Dictionary<string, int>(dict1.Count);   // Thanks, @mjwills!
    foreach (var pair in dict1)
    {
        dict3.Add(pair.Key, pair.Value - dict2[pair.Key]);
    }
    

    或者使用 ToDictionary 方法而不是循环:

    var dict3 = dict1.ToDictionary(p => p.Key, p => p.Value - dict2[p.Key]);
    
        2
  •  2
  •   Enigmativity    5 年前

    如果其中一个或两个词典中有多余的键,则此代码将它们合并在一起:

    var dict3 =
        dict1
            .Concat(dict2)
            .Select(x => x.Key)
            .Distinct()
            .Select(x => new
            {
                Key = x,
                Value1 = dict1.TryGetValue(x, out int Value1) ? Value1 : 0,
                Value2 = dict2.TryGetValue(x, out int Value2) ? Value2 : 0,
            })
            .ToDictionary(x => x.Key, x => x.Value1 - x.Value2);
    

    这仍然适用于您的原始词典,但如果您有:

    var dict1 = new Dictionary<string, int>()
    {
        { "alex", 10 },
        { "liza", 10 },
        { "harry", 20 },
    };
    
    var dict2 = new Dictionary<string, int>()
    {
        { "alex", 5 },
        { "liza", 4 },
        { "mike", 1 },
    };
    

    然后输出如下所示的字典:

    new Dictionary<string, int>()
    {
        { "alex", 5 },
        { "liza", 6 },
        { "harry", 20 },
        { "mike", -1 },
    };
    
        3
  •  1
  •   live627    5 年前

    一个简单的方法是通过一个简单的foreach循环。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApp3
    {
        class Program
        {
            static void Main(string[] args)
            {
                var first = new Dictionary<string, int> { { "alex", 10 }, { "liza", 10 }, { "harry", 20 } };
                var second = new Dictionary<string, int> { { "alex", 5 }, { "liza", 4 }, { "harry", 1 } };
                var third = first.ToDictionary(entry => entry.Key, entry => entry.Value);
    
                foreach (var item in first)
                {
                    third[item.Key] = first[item.Key] - second[item.Key];
                    Console.WriteLine(third[item.Key]);
                }
    
                Console.Read();
            }
        }
    }
    

    ... 或通过linq

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApp3
    {
        class Program
        {
            static void Main(string[] args)
            {
                var first = new Dictionary<string, int> { { "alex", 10 }, { "liza", 10 }, { "harry", 20 } };
                var second = new Dictionary<string, int> { { "alex", 5 }, { "liza", 4 }, { "harry", 1 } };
                var third = first.ToDictionary(entry => entry.Key, entry => entry.Value - second[entry.Key]);
    
                foreach (var item in first)
                {
                    Console.WriteLine(third[item.Key]);
                }
    
                Console.Read();
            }
        }
    }