【问题标题】:how to swap keys into values and values into key in a dictionary如何将键交换为值并将值交换为字典中的键
【发布时间】:2018-08-24 12:10:55
【问题描述】:

我想交换字典中的键和值

我的字典是这样的

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 表达式它改变了,但我需要一些其他的方法来做..

【问题讨论】:

  • 您想交换实际字典中的键和值吗?还是只是打印交换? "键 = {1},值 = {0}",
  • 您确定所有值都是唯一的(字典“合同”不需要它们)?如果不是,您希望这种情况如何处理?
  • 如果多个键的值相同怎么办?
  • 不知道你为什么想要这个交换。按原样使用字典。您是否正在尝试解决测试?
  • 我想换成实际的字典@JoshAdams

标签: c# dictionary swap


【解决方案1】:

假设值是唯一的,这可以工作:

var dic = new Dictionary<int,int>{{1,10}, {2, 20}, {3, 30}};

var dic2 = dic.ToDictionary(x => x.Value, x=> x.Key);

它实际上不会交换边,你会得到一本新字典。

【讨论】:

  • 兄弟,我不想创建新字典。实际字典中的值交换
  • 你不能。为什么你认为你需要那个?如果双方属于同一类型,您可以尝试在现有条目之后添加“交换”条目,然后删除它们。但是你什么也得不到。
  • Key 是只读的。您可以做的是在创建这个附加字典后,您可以清除原始字典并添加新字典中的所有条目。
【解决方案2】:

我想你会理解下面的代码:

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();
    }
  }
}

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 2010-11-08
    • 2021-07-21
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多