如何将键交换为值和将值交换为字典中的键
how to swap keys into values and values into key in a dictionary
伙计们,我想交换字典中的键和值
我的字典是这样的
public static void Main(string[] args)
{
Dictionary<int,int> dic = new Dictionary<int,int>{{1,10}, {2, 20}, {3, 30}};
}
现在我正在打印字典中的值
foreach (KeyValuePair<int, int> kvp in dic)
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
Console.ReadKey();
}
在展示中我得到了这个
Key = 1, Value = 10
Key = 2, Value = 20
Key = 3, Value = 30
我想在键和值之间交换值..交换后答案应该是这样的
Key = 10, Value = 1
Key = 20, Value = 2
Key = 30, Value = 3
我做了 lambda 表达式,它改变了,但我需要一些其他方法来做..
假设值是唯一的,这可以工作:
var dic = new Dictionary<int,int>{{1,10}, {2, 20}, {3, 30}};
var dic2 = dic.ToDictionary(x => x.Value, x=> x.Key);
它实际上不会换边,你会得到一本新词典。
我想你会理解下面的代码:
namespace Swap
{
public class Class1
{
public SortedDictionary<string, string> names;
public void BeforeSwaping()
{
Console.WriteLine("Before Swapping: ");
Console.WriteLine();
names = new SortedDictionary<string, string>();
names.Add("Sonoo", "srinath");
names.Add("Perer", "mahesh");
names.Add("James", "ramesh");
names.Add("Ratan", "badri");
names.Add("Irfan", "suresh");
names.Add("sravan", "current");
foreach (KeyValuePair<string, string> item in names)
{
Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value);
}
Console.WriteLine();
}
public void AfterSwaping()
{
Console.WriteLine("After Swapping: ");
Console.WriteLine();
var key = names.Keys;
var val = names.Values;
string[] arrayKey = new string[key.Count];
string[] arrayVal = new string[val.Count];
int i = 0;
foreach (string s in key)
{
arrayKey[i++] = s;
}
int j = 0;
foreach (string s in val)
{
arrayVal[j++] = s;
}
names.Clear();
//Console.WriteLine(arrayVal[0] + " " + arrayKey[0]);
for (int k = 0; k < (arrayKey.Length + arrayVal.Length) / 2; k++)
{
names.Add(arrayVal[k], arrayKey[k]);
}
foreach (KeyValuePair<string, string> s in names)
{
Console.WriteLine("key:"+s.Key + ", "+"value:" + s.Value);
}
}
public static void Main(string[] args)
{
Class1 c = new Class1();
c.BeforeSwaping();
c.AfterSwaping();
Console.ReadKey();
}
}
}
伙计们,我想交换字典中的键和值
我的字典是这样的
public static void Main(string[] args)
{
Dictionary<int,int> dic = new Dictionary<int,int>{{1,10}, {2, 20}, {3, 30}};
}
现在我正在打印字典中的值
foreach (KeyValuePair<int, int> kvp in dic)
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
Console.ReadKey();
}
在展示中我得到了这个
Key = 1, Value = 10
Key = 2, Value = 20
Key = 3, Value = 30
我想在键和值之间交换值..交换后答案应该是这样的
Key = 10, Value = 1
Key = 20, Value = 2
Key = 30, Value = 3
我做了 lambda 表达式,它改变了,但我需要一些其他方法来做..
假设值是唯一的,这可以工作:
var dic = new Dictionary<int,int>{{1,10}, {2, 20}, {3, 30}};
var dic2 = dic.ToDictionary(x => x.Value, x=> x.Key);
它实际上不会换边,你会得到一本新词典。
我想你会理解下面的代码:
namespace Swap
{
public class Class1
{
public SortedDictionary<string, string> names;
public void BeforeSwaping()
{
Console.WriteLine("Before Swapping: ");
Console.WriteLine();
names = new SortedDictionary<string, string>();
names.Add("Sonoo", "srinath");
names.Add("Perer", "mahesh");
names.Add("James", "ramesh");
names.Add("Ratan", "badri");
names.Add("Irfan", "suresh");
names.Add("sravan", "current");
foreach (KeyValuePair<string, string> item in names)
{
Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value);
}
Console.WriteLine();
}
public void AfterSwaping()
{
Console.WriteLine("After Swapping: ");
Console.WriteLine();
var key = names.Keys;
var val = names.Values;
string[] arrayKey = new string[key.Count];
string[] arrayVal = new string[val.Count];
int i = 0;
foreach (string s in key)
{
arrayKey[i++] = s;
}
int j = 0;
foreach (string s in val)
{
arrayVal[j++] = s;
}
names.Clear();
//Console.WriteLine(arrayVal[0] + " " + arrayKey[0]);
for (int k = 0; k < (arrayKey.Length + arrayVal.Length) / 2; k++)
{
names.Add(arrayVal[k], arrayKey[k]);
}
foreach (KeyValuePair<string, string> s in names)
{
Console.WriteLine("key:"+s.Key + ", "+"value:" + s.Value);
}
}
public static void Main(string[] args)
{
Class1 c = new Class1();
c.BeforeSwaping();
c.AfterSwaping();
Console.ReadKey();
}
}
}