我正在尝试创建一个字典,其中的键将是预先存在的List<string>
中的元素,值将为List<List<string>>
,如下所示:
List<string> newDictKeys = new List<string>(new []{"12323","432234","45345435"});
List<string> listVal1 = new List<string>(new []{"dfgdfg","asdfds","wertert"});
List<string> listVal2 = new List<string>(new []{"ZCxzcx","xcvbcvb","gfhjfgj"});
List<List<string>> dictVals = new List<List<string>>();
Dictionary<string,List<List<string>> dict = new Dictionary<string,List<List<string>>();
有没有简单,优雅的方法来做到这一点?
基本上这就是映射:
"12323" -> ["dfgdfg","ZCxzcx"]
"432234" -> ["asdfds","xcvbcvb"]
"45345435" -> ["wertert","gfhjfgj"]
答案 0 :(得分:2)
根据问题更新更新了答案 好的,所以你想要每个值列表中的第n个项目作为值,如下所示:
List<string> newDictKeys = new List<string>(new []{"12323","432234","45345435"});
List<string> listVal1 = new List<string>(new []{"dfgdfg","asdfds","wertert"});
List<string> listVal2 = new List<string>(new []{"ZCxzcx","xcvbcvb","gfhjfgj"});
List<List<string>> dictVals = new List<List<string>>();
dictVals.Add(listVal1);
dictVals.Add(listVal2);
Dictionary<string, List<string>> dict =
newDictKeys.Select((key, index) => new {key, index})
.ToDictionary(entry => entry.key,
entry => new List<string>(){dictVals[0][entry.index], dictVals[1][entry.index]});
基本上,它遍历newDictKeys
中的所有键,创建一个临时匿名对象,其中键和该键的索引进入newDictKeys
列表(所以{"12323", 0}, {"432234", 1}, ...
)。然后,它创建一个Dictionary,其中键是来自newDictKeys
的值,对于该值,它从同一索引位置的dictValues
中的每个子列表中获取值。
或者,简化版(直接访问listVal1
和listVal2
集合,而不是dictVals
:
Dictionary<string, List<string>> dict =
newDictKeys.Select((key, index) => new {key, index})
.ToDictionary(entry => entry.key,
entry => new List<string>(){listVal1[entry.index], listVal2[entry.index]});
原始答案
如果您想为newDictKeys
中的每个条目设置相同的值集,则可以将其映射为:
List<string> newDictKeys = new List<string>(new []{"12323","432234","45345435"});
List<string> listVal1 = new List<string>(new []{"dfgdfg","asdfds","wertert"});
List<string> listVal2 = new List<string>(new []{"ZCxzcx","xcvbcvb","gfhjfgj"});
List<List<string>> dictVals = new List<List<string>>();
dictVals.Add(listVal1);
dictVals.Add(listVal2);
Dictionary<string, List<List<string>>> dict =
newDictKeys.ToDictionary(key => key, key => dictVals);
如果您想为newDictKeys
中的每个条目设置不同的值集,请将值放入数组中,以便它们与键的顺序相同,您可以使用.Select()
获取键列表中键的索引,然后.ToDictionary()
映射到您想要的值,如下所示:
var valuesArray = new []{dictVals, dictVals2, ...};
Dictionary<string, List<List<string>>> dict =
newDictKeys.Select((key, index) => new {key, index})
.ToDictionary(entry => entry.key, entry => valuesArray[entry.index]);
valuesArray
也可以是一个列表,只要它的值可以由索引器检索。
答案 1 :(得分:2)
由于您将为同一个密钥存储两个列表,为什么不将两个列表组合在一起?
List<string> newDictKeys = new List<string>{ "12323", "432234", "45345435"};
List<string> t = new List<string> { "dfgdfg", "asdfds", "wertert" };
List<string> t2 = new List<string>{ "ZCxzcx", "xcvbcvb", "gfhjfgj" };
t.AddRange(t2);
Dictionary<string,List<string>> dict = newDictKeys.ToDictionary(key => key, key => t);
<强>更新强>
使用对象列表:
List<string> newDictKeys = new List<string> { "12323", "432234", "45345435" };
List<string> t = new List<string> { "dfgdfg", "asdfds", "wertert" };
List<string> t2 = new List<string> { "ZCxzcx", "xcvbcvb", "gfhjfgj" };
Dictionary<string, List<object>> dict = newDictKeys.ToDictionary(key => key, key => new List<object> {t, t2});
更新2 这应该成为一个技巧:
List<string> newDictKeys = new List<string> { "12323", "432234", "45345435" };
List<string> t = new List<string> { "dfgdfg", "asdfds", "wertert" };
List<string> t2 = new List<string> { "ZCxzcx", "xcvbcvb", "gfhjfgj" };
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
foreach (var key in newDictKeys.Where(key => t.Count > 0 && t2.Count > 0))
{
dict.Add(key, new List<string> {t.FirstOrDefault(), t2.FirstOrDefault()});
t.RemoveAt(0);
t2.RemoveAt(0);
}
更新3 使用队列
List<string> newDictKeys = new List<string> { "12323", "432234", "45345435" };
Queue<string> t = new Queue<string> (new []{ "dfgdfg", "asdfds", "wertert" });
Queue<string> t2 = new Queue<string>(new[] { "ZCxzcx", "xcvbcvb", "gfhjfgj" });
Dictionary<string, List<string>> dict = newDictKeys
.Where(key => t.Count > 0 && t2.Count > 0)
.ToDictionary(key => key, key => new List<string> {t.Dequeue(), t2.Dequeue()});