[英]How to use a list<string> as the keys in a dictionary
I'm trying to create a dictionary where the keys will be the elements in a pre-existing List<string>
and the values will be List<List<string>>
like this: 我正在尝试创建一个字典,其中的键将是预先存在的
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>>();
Is there any simple, elegant way to do this? 有没有简单,优雅的方法可以做到这一点?
Essentially this would be the mapping: 本质上,这将是映射:
"12323" -> ["dfgdfg","ZCxzcx"]
"432234" -> ["asdfds","xcvbcvb"]
"45345435" -> ["wertert","gfhjfgj"]
Updated Answer based on question update Ok, so you want the nth item from each value list as the values, which would look like this: 根据问题更新了答案,好了,所以您希望将每个值列表中的第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]});
Basically, it iterates through all the keys in newDictKeys
, creates a temporary anonymous object with the key and the index of that key into the newDictKeys
list (so {"12323", 0}, {"432234", 1}, ...
). 基本上,它会遍历
newDictKeys
所有键,创建一个临时的匿名对象,并带有该键以及该键在newDictKeys
列表中的索引(因此{"12323", 0}, {"432234", 1}, ...
)。 Then, it creates a Dictionary where the key is the value from newDictKeys
and for the value it gets the values from each sub-list in dictValues
at the same index location. 然后,它创建一个Dictionary,其中的键是
newDictKeys
中的值,对于该值,它从dictValues
中相同索引位置的每个子列表中获取值。
Or, a simplified version (directly access the listVal1
and listVal2
collections instead of dictVals
: 或者,使用简化版本(直接访问
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]});
Original Answer If you want the same set of values for each entry in newDictKeys
, you could map it like this: 原始答案如果要为
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);
If you want a different set of values for each entry in newDictKeys
, put the values into an array as well, so that they are in the same order as the keys, you could use .Select()
to get the index of the key in the key list and then .ToDictionary()
to map to the values you want, like this: 如果要为
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]);
The valuesArray
could also be a list, as long as it's values can be retrieved by an indexer. valuesArray
也可以是一个列表,只要索引器可以检索它的值即可。
Since You will have two lists stored for the same key why not to combine two lists together? 由于您将为同一密钥存储两个列表,为什么不将两个列表组合在一起?
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);
UPDATE 更新
With the list of objects: 带有对象列表:
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});
UPDATE 2 This should make a trick: 更新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);
}
UPDATE 3 With Queue UPDATE 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()});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.