RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1252893
Accepted
polsok
polsok
Asked:2022-03-07 23:15:17 +0000 UTC2022-03-07 23:15:17 +0000 UTC 2022-03-07 23:15:17 +0000 UTC

字典中的一个键获取多个值

  • 772

有一本字典:

Dictionary<string, string> AuthorList = new Dictionary<string, string>();
AuthorList.Add("aaa", "гистидин");
AuthorList.Add("bbb", "фенилаланин");
AuthorList.Add("aaa", "кокаин");
AuthorList.Add("ccc", "глицин");
AuthorList.Add("ddd", "глицин");
AuthorList.Add("aaa", "атропин");

如何通过键aaa获取所有的值(以列表或数组的形式)?

c#
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. VladD
    2022-03-08T04:46:05Z2022-03-08T04:46:05Z

    你需要一个字典,可以为一个键存储几个不同的值。此类字典通常命名为MultiDictionary.

    例如,您可以MultiValueDictionary从 nuget 包中获取 Microsoft 版本Microsoft.Experimental.Collections(小心,它是预发布版!)。包含包(您必须选中包含预发布复选框),您的代码将如下所示:

    MultiValueDictionary<string, string> AuthorList = new();
    AuthorList.Add("aaa", "гистидин");
    AuthorList.Add("bbb", "фенилаланин");
    AuthorList.Add("aaa", "кокаин");
    AuthorList.Add("ccc", "глицин");
    AuthorList.Add("ddd", "глицин");
    AuthorList.Add("aaa", "атропин");
    
    var results = AuthorList["aaa"];
    foreach (var result in results)
        Console.WriteLine(result);
    

    在控制台上

    组氨酸
    可卡因
    阿托品

    • 5
  2. Best Answer
    Максим Фисман
    2022-03-08T01:46:23Z2022-03-08T01:46:23Z

    为什么对你不起作用

    由于您的是Dictionary < stringAuthorList , string>数据类型,因此它由KeyValuePair对组成,其中键是字符串,值是字符串。因此,当您多次向具有相同键的单元格写入值时,旧的会被简单地擦除(如在常规变量中)。例子:

    Dictionary<string, string> dict = new Dictionary<string, string>();
    dict["Key1"] = "Value1";
    Console.WriteLine(dict["Key1"]);
    dict["Key1"] = "Value2";
    Console.WriteLine(dict["Key1"]);
    dict["Key1"] = "Value3";
    Console.WriteLine(dict["Key1"]);
    

    结论:

    Value1
    Value2
    Value3
    

    该怎么办?

    如果你想一次为每个键存储多个值,你的选择是List。

    在这种情况下,字典将像这样创建:

    Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
    

    具体来说, 您的任务可以实现如下:

    Dictionary<string, List<string>> AuthorDict = new Dictionary<string, List<string>>();
    
    AuthorDict["aaa"] = new List<string>();
    AuthorDict["bbb"] = new List<string>();
    AuthorDict["ccc"] = new List<string>();
    
    AuthorList["aaa"].Add("гистидин");
    AuthorList["bbb"].Add("фенилаланин");
    AuthorList["aaa"].Add("кокаин");
    AuthorList["ccc"].Add("глицин");
    AuthorList["ddd"].Add("глицин");
    AuthorList["ddd"].Add("атропин");
    

    以后可以通过key访问值列表或特定值,如下:

    AuthorList["bbb"]; // Список по ключу "bbb"
    AuthorList["aaa"][1]; // Первый элемент из списка по ключу "aaa"
    

    此外

    一般来说,字典是一个KeyValuePair<TKey, TValue> 结构,即键和值都可以是任何数据类型。因此,根据您的愿望和目标,您可以使用两个Queue来获得排队功能,一个HashSet,甚至另一个Dictionary。例如:

    Dictionary<string, Queue<string>> QueuedDictionary = new Dictionary<string, Queue<string>>();
    Dictionary<string, HashSet<string>> HashsetDictionary = new Dictionary<string, HashSet<string>>();
    Dictionary<string, Dictionary<int, string>> DoubleDictionary = new Dictionary<string, Dictionary<int, string>>();
    

    所有这些都可以称为具有附加功能的 List,但由于 你没有在你的问题中提到它们,我将停止这个话题


    PS Как мне по ключу aaa получить все значения (в виде списка или массива)? - 用数组实现这一点很困难,因为它的长度不是动态的,所以可能很难初始化或读取......

    • 4
  3. EzikBro
    2022-03-08T00:48:49Z2022-03-08T00:48:49Z

    每个键的值都存储在一个 List 中,因此它们可以重复:

    Dictionary<string, List<string>> AuthorDict = new Dictionary<string, List<string>>();
    
    AuthorDict["aaa"] = new List<string>();
    AuthorDict["bbb"] = new List<string>();
    AuthorDict["ccc"] = new List<string>();
    
    AuthorDict["aaa"].Add("1");
    AuthorDict["bbb"].Add("2");
    AuthorDict["aaa"].Add("3");
    AuthorDict["ccc"].Add("4");
    AuthorDict["bbb"].Add("2");
    
    foreach (var item in AuthorDict)
        Console.WriteLine($"{item.Key}: {String.Join(", ", item.Value)}");
    

    结论:

    aaa: 1, 3
    bbb: 2, 2
    ccc: 4
    

    每个键的值都存储在一个 HashSet 中,因此它们将是唯一的:

    Dictionary<string, HashSet<string>> AuthorDict = new Dictionary<string, HashSet<string>>();
    
    AuthorDict["aaa"] = new HashSet<string>();
    AuthorDict["bbb"] = new HashSet<string>();
    AuthorDict["ccc"] = new HashSet<string>();
    
    AuthorDict["aaa"].Add("1");
    AuthorDict["bbb"].Add("2");
    AuthorDict["aaa"].Add("3");
    AuthorDict["ccc"].Add("4");
    AuthorDict["bbb"].Add("2");
    
    foreach (var item in AuthorDict)
        Console.WriteLine($"{item.Key}: {String.Join(", ", item.Value)}");
    

    结论:

    aaa: 1, 3
    bbb: 2
    ccc: 4
    
    • 2

相关问题

  • 使用嵌套类导出 xml 文件

  • 分层数据模板 [WPF]

  • 如何在 WPF 中为 ListView 手动创建列?

  • 在 2D 空间中,Collider 2D 挂在玩家身上,它对敌人的重量相同,我需要它这样当它们碰撞时,它们不会飞向不同的方向。统一

  • 如何在 c# 中使用 python 神经网络来创建语音合成?

  • 如何知道类中的方法是否属于接口?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5