You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检索Dictionary A的值到Dictionary B并分类存储匹配/未匹配键值对

字典值匹配实现方案

需求说明

我有两个Dictionary<string, List<string>>类型的字典A和B,需要遍历A中所有元素值,在B中检索该值是否存在:

  • 若值存在,将对应的键值对(A的键 + 该值)存入matchFound字典(同一键对应多个值用列表存储)
  • 若值不存在,将对应的键值对存入noMatchFound字典

示例场景:

  • A中path1的value2在B中存在,matchFound["path1"]会添加value2;value3也存在,最终matchFound["path1"]包含["value2", "value3"]
  • A中path3的value7在B中不存在,noMatchFound["path3"]会添加value7

优化实现思路

为提升检索效率,先把B中所有值提取到HashSet<string>中,这样判断值是否存在的时间复杂度为O(1),避免每次检索都遍历B的所有列表。

完整代码实现

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        // 初始化字典A
        Dictionary<string, List<string>> A = new Dictionary<string, List<string>>();
        A.Add("path1", new List<string> { "value1", "value2", "value3" });
        A.Add("path2", new List<string> { "value4", "value5", "value6" });
        A.Add("path3", new List<string> { "value7", "value1", "value4" });

        // 初始化字典B
        Dictionary<string, List<string>> B = new Dictionary<string, List<string>>();
        B.Add("path4", new List<string> { "value11", "value2", "value3" });
        B.Add("path5", new List<string> { "value4", "value51", "value61" });
        B.Add("path6", new List<string> { "value71", "value1", "value4" });

        // 初始化结果字典(修正原代码中的类型错误)
        Dictionary<string, List<string>> matchFound = new Dictionary<string, List<string>>();
        Dictionary<string, List<string>> noMatchFound = new Dictionary<string, List<string>>();

        // 提取B中所有值到HashSet,优化检索速度
        HashSet<string> allValuesInB = new HashSet<string>(B.SelectMany(kv => kv.Value));

        // 遍历字典A的每个键值对
        foreach (var kvp in A)
        {
            string key = kvp.Key;
            foreach (string value in kvp.Value)
            {
                if (allValuesInB.Contains(value))
                {
                    // 处理匹配到的情况
                    if (!matchFound.ContainsKey(key))
                    {
                        matchFound[key] = new List<string>();
                    }
                    matchFound[key].Add(value);
                }
                else
                {
                    // 处理未匹配到的情况
                    if (!noMatchFound.ContainsKey(key))
                    {
                        noMatchFound[key] = new List<string>();
                    }
                    noMatchFound[key].Add(value);
                }
            }
        }

        // 输出测试结果(可选)
        Console.WriteLine("matchFound 内容:");
        foreach (var kvp in matchFound)
        {
            Console.WriteLine($"{kvp.Key}: [{string.Join(", ", kvp.Value)}]");
        }

        Console.WriteLine("\nnoMatchFound 内容:");
        foreach (var kvp in noMatchFound)
        {
            Console.WriteLine($"{kvp.Key}: [{string.Join(", ", kvp.Value)}]");
        }
    }
}

代码说明

  1. HashSet构建:通过SelectMany扁平化B的所有值,存入HashSet实现快速存在性判断
  2. 遍历A的元素:逐个处理A中每个键对应的每个值
  3. 结果字典处理:
    • 若值在B中存在,检查matchFound是否已有对应键,无则创建新列表,再添加值
    • 若值不存在,同理处理noMatchFound字典
  4. 修正原代码错误:原代码中matchFound和noMatchFound的初始化类型错误,已修正为Dictionary<string, List<string>>

输出结果

运行代码后,输出如下:

matchFound 内容:
path1: [value1, value2, value3]
path2: [value4]
path3: [value1, value4]

noMatchFound 内容:
path2: [value5, value6]
path3: [value7]

内容的提问来源于stack exchange,提问作者Ustaad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 20:25:40