如何切换字典中所有值的顺序并反转它的顺序?
How to switch order of all values in a dictionary and reverse the order it?
我没有完成...
我有代码:
Dictionary<string, string>[] dic = new Dictionary<string, string>[2];
dic[0].Add("10", "a");
dic[1].Add("20", "b");
我应该输出 Console.Writeline:
10, b
20, a
20, a
10, b
这意味着我应该首先更改值,然后更改密钥,但我不知道如何管理它。我试过微软的官方网站,但我没有进一步。
谁能帮帮我?
您的代码无法编译
不过也许你想要这样的东西
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("10", "a");
dic.Add("20", "b");
// Ouput
foreach (var key in dic.Keys)
Console.WriteLine(key + " "+ dic[key]);
// Change
dic["10"] = "C";
dic["20"] = "D";
// Ouput
foreach (var key in dic.Keys)
Console.WriteLine(key + " " + dic[key]);
或者您可能只想要一个列表
var list = new List<(string,string)>();
list.Add(("10", "a"));
list.Add(("20", "b"));
// order one way
foreach (var item in list.OrderBy(x => x.Item1))
Console.WriteLine(item);
// order another
foreach (var item in list.OrderByDescending(x => x.Item1))
Console.WriteLine(item);
输出
10 a
20 b
10 C
20 D
(10, a)
(20, b)
(20, b)
(10, a)
首先我要说这个要求不应该用字典来解决,因为它不是一个有序的集合。这意味着您不应该依赖键的字典顺序,因为如果您添加或删除对,它可能会改变,并且它可能会随着 .NET 的下一版本而改变。
但是,如果这只是一个练习。您可以使用 List<T>
通过索引访问它:
Dictionary<string, string> dict = new Dictionary<string, string>() {{"10", "a"}, {"20", "b"}};
List<KeyValuePair<string, string>> listDict = dict.ToList();
// 1.) switch the values, last becomes first and vice-versa
for (int i = 0; i < listDict.Count; i++)
{
string oppositeValue = listDict[listDict.Count - 1 - i].Value;
dict[listDict[i].Key] = oppositeValue;
}
// 2.) reverse the dictionary to "switch" the keys, this is not recommended with a dictionary, because it is not an ordered collection
dict = dict.Reverse().ToDictionary(kv => kv.Key, kv => kv.Value);
我没有完成...
我有代码:
Dictionary<string, string>[] dic = new Dictionary<string, string>[2];
dic[0].Add("10", "a");
dic[1].Add("20", "b");
我应该输出 Console.Writeline:
10, b
20, a
20, a
10, b
这意味着我应该首先更改值,然后更改密钥,但我不知道如何管理它。我试过微软的官方网站,但我没有进一步。
谁能帮帮我?
您的代码无法编译
不过也许你想要这样的东西
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("10", "a");
dic.Add("20", "b");
// Ouput
foreach (var key in dic.Keys)
Console.WriteLine(key + " "+ dic[key]);
// Change
dic["10"] = "C";
dic["20"] = "D";
// Ouput
foreach (var key in dic.Keys)
Console.WriteLine(key + " " + dic[key]);
或者您可能只想要一个列表
var list = new List<(string,string)>();
list.Add(("10", "a"));
list.Add(("20", "b"));
// order one way
foreach (var item in list.OrderBy(x => x.Item1))
Console.WriteLine(item);
// order another
foreach (var item in list.OrderByDescending(x => x.Item1))
Console.WriteLine(item);
输出
10 a
20 b
10 C
20 D
(10, a)
(20, b)
(20, b)
(10, a)
首先我要说这个要求不应该用字典来解决,因为它不是一个有序的集合。这意味着您不应该依赖键的字典顺序,因为如果您添加或删除对,它可能会改变,并且它可能会随着 .NET 的下一版本而改变。
但是,如果这只是一个练习。您可以使用 List<T>
通过索引访问它:
Dictionary<string, string> dict = new Dictionary<string, string>() {{"10", "a"}, {"20", "b"}};
List<KeyValuePair<string, string>> listDict = dict.ToList();
// 1.) switch the values, last becomes first and vice-versa
for (int i = 0; i < listDict.Count; i++)
{
string oppositeValue = listDict[listDict.Count - 1 - i].Value;
dict[listDict[i].Key] = oppositeValue;
}
// 2.) reverse the dictionary to "switch" the keys, this is not recommended with a dictionary, because it is not an ordered collection
dict = dict.Reverse().ToDictionary(kv => kv.Key, kv => kv.Value);