我有字典,其中包含键和值作为列表 [英] i have dictionary which contain the key and Value as a list

查看:87
本文介绍了我有字典,其中包含键和值作为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi


i有一个字典,其中包含键作为字符串,值为列表

所以如何获取值od特定键

我想从该值替换certatin字符串作为列表。

解决方案

如果你的意思是你有这个:

字典<字符串,列表<字符串>> myDict = ... 

然后它相对容易:

 List< string> values = myDict [  my key]; 
值[ 2 ] = 新值字符串;



 myDict [  my key] [ 2 ] =  < span class =code-string> new value string; 


试试这个





字典<字符串,列表<字符串>> dict =  new 字典< string,List< string>>(); 
dict.Add( one new string [] { aaa bb nnn dd } .ToList());
dict.Add( two new string [] { d bb cc uuu } .ToList());
dict.Add( three new string [] { affaa bb cc gggdd } .ToList());
dict.Add( four new string [] { adfdaa xxbb ccg dd } .ToList());

string key = 一个;
List< string> Value = dict [key];
string replacefrom = aaa;
string replaceTo = apple;
Value [Value.IndexOf(replacefrom)] = replaceTo;



现在字典将使用新项目进行更新,因为它是一个参考输入


hi
i have a dictionary which contain the key as a string and value as the list
so how to get the value od particular key
and i want to replace certatin string from that value which is as a list.

解决方案

If you mean you have this:

Dictionary<string, List<string>> myDict = ...

then its relatively easy:

List<string> values = myDict["my key"];
values[2] = "new value string";

Or

myDict["my Key"][2] = "new value string";


Try this


Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
            dict.Add("one", new string[] { "aaa", "bb", "nnn", "dd" }.ToList());
            dict.Add("two", new string[] { "d", "bb", "cc", "uuu" }.ToList());
            dict.Add("three", new string[] { "affaa", "bb", "cc", "gggdd" }.ToList());
            dict.Add("four", new string[] { "adfdaa", "xxbb", "ccg", "dd" }.ToList());

            string key = "one";
           List<string> Value =  dict[key];
           string replacefrom = "aaa";
           string replaceTo = "apple";
           Value[Value.IndexOf(replacefrom)] = replaceTo;


now the dictionary will be updated with the new item, since it is a reference Type


这篇关于我有字典,其中包含键和值作为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆