【问题标题】:Failing to print duplicates from List of Dictionary无法从字典列表中打印重复项
【发布时间】:2017-02-20 13:32:23
【问题描述】:

我是编程新手,但已经掌握了窍门。但我一直在尝试从字典列表中打印重复项,这给了我以下错误:

List<Dictionary<string, string>> Locations = new List<Dictionary<string, string>>();
        var stringinput = new Dictionary<string, string>();
        string xString = x.ToString();
        string yString = y.ToString();            
        stringinput.Add(xString, yString);

        Locations.Add(stringinput);

        var duplicates = Locations
        .GroupBy(i => i)
        .Where(g => g.Count() > 1)
        .Select(g => g.Key);

        foreach (var d in duplicates)
            Console.WriteLine(d);

是否有人对打印这些副本的解决方案或变通方法有任何想法?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Regex;
 
namespace Application
{
    static class EmptyClass
    {
        static void ForEach(this int[] ints, Action<int> action)
        {
            foreach (int i in ints)
                action(i);
        }
 
        static void Main()
        {
            int x = 0;
            int y = 0;
            string xString = x.ToString();
            string yString = y.ToString();
            string direction = "north";
 
            List<Dictionary<string, string>> Locations = new List<Dictionary<string, string>>();
            var stringinput = new Dictionary<string, string>();
            stringinput.Add(xString, yString);
            Console.WriteLine("Insert directions(R1, R2, L3 etc...: ");
            string s = Console.ReadLine();
            string[] words = s.Split(',');
            foreach (string word in words)
            {
                var Rwords = word;
                if (Regex.IsMatch(Rwords, "R"))
                {
                    Regex re = new Regex(@"\d+");
                    Match m = re.Match(Rwords);
 
                    if (direction == "north")
                    {
                        x = x + Convert.ToInt32(m.Value);
                        direction = "east";
                        Console.WriteLine("Your location is (x{0},y{1})", x, y);
                        Locations.Add(stringinput);
                    }
                    else if (direction == "east")
                    {
                        y = y - Convert.ToInt32(m.Value);
                        direction = "south";
                        Console.WriteLine("Your location is (x{0},y{1})", x, y);
                        Locations.Add(stringinput);
                    }
                    else if (direction == "south")
                    {
                        x = x - Convert.ToInt32(m.Value);
                        direction = "west";
                        Console.WriteLine("Your location is (x{0},y{1})", x, y);
                        Locations.Add(stringinput);
                    }
                    else if (direction == "west")
                    {
                        y = y + Convert.ToInt32(m.Value);
                        direction = "north";
                        Console.WriteLine("Your location is (x{0},y{1})", x, y);
                        Locations.Add(stringinput);
                    }
                }
                var Lwords = word;
                if (Regex.IsMatch(Lwords, "L"))
                {
                    Regex re = new Regex(@"\d+");
                    Match m = re.Match(Rwords);
 
                    if (direction == "north")
                    {
                        x = x - Convert.ToInt32(m.Value);
                        direction = "west";
                        Console.WriteLine("Your location is (x{0},y{1})", x, y);
                        Locations.Add(stringinput);
                    }
 
                    else if (direction == "west")
                    {
                        y = y - Convert.ToInt32(m.Value);
                        direction = "south";
                        Console.WriteLine("Your location is (x{0},y{1})", x, y);
                        Locations.Add(stringinput);
                    }
                    else if (direction == "south")
                    {
                        x = x + Convert.ToInt32(m.Value);
                        direction = "east";
                        Console.WriteLine("Your location is (x{0},y{1})", x, y);
                        Locations.Add(stringinput);
                    }
                    else if (direction == "east")
                    {
                        y = y + Convert.ToInt32(m.Value);
                        direction = "north";
                        Console.WriteLine("Your location is (x{0},y{1})", x, y);
                        Locations.Add(stringinput);
                    }
                }
            }
            int Distance = x + y;
            Distance *= -1;
            Console.WriteLine("Calculating distance to HQ......");
            Console.WriteLine("Your location is (x{0},y{1}) wich is {2} Blocks away from Easter Bunny HQ", x, y, Distance, ",");
            var duplicates = Locations
            .GroupBy(i => i)
            .Where(g => g.Count() > 1)
            .Select(g => g.Key);
 
            foreach (var d in duplicates)
                Console.WriteLine(d);
 
        }
    }
}

【问题讨论】:

  • 这是因为两个字典对象不会比较相等性,即使它们的内容相同。
  • 根据什么标准来查找重复项?
  • 在这里发布所有相关代码,外部链接腐烂。因此,问题必须独立存在。
  • 为什么要将位置存储在字典中?字典用于具有键和值的数据,您可以使用键来查找值。我会定义一个 Coordinate 类并列出这些类,或者使用 Tuple。我认为编译器不理解字典中“重复”的概念。
  • @A.T.当之前输入了相同的 x&y 时。因此,例如,当 34,24 已添加到列表中时,25,34,以及当 34,24 第二次添加到列表中时 -> 这就是我要查找的重复项。

标签: c# list dictionary duplicates


【解决方案1】:

您可以使用SelectMany 来展平所有字典中的键值对:

duplicates = locations.SelectMany(dict => dict)
                      .GroupBy(kv => kv.Key)
                      .Where(g => g.Count() > 1)
                      .Select(g => g.Key);

您目前正在按字典本身进行分组。这不起作用,因为Dictionary&lt;tkey,tval&gt; 不会覆盖Equals+GetHashCode。您也不想找到重复的字典而是重复的键。

【讨论】:

  • 感谢您的回答。奇怪的输出消失了,变成了 0。可能出了什么问题?或者当 x 与 y 相同时,上面的代码是否会打印?此代码的目的是获取已“访问”的重复位置。例如 x34,y24 已添加到列表中,x25,y34,当 x34,y24 第二次添加到列表中时。
  • @CodeCommisaris:好吧,在您当前的代码中没有重复
猜你喜欢
  • 1970-01-01
  • 2018-04-30
  • 2022-08-08
  • 1970-01-01
  • 2021-09-01
  • 2021-06-06
  • 1970-01-01
  • 2012-02-16
相关资源
最近更新 更多