无法从词典列表中打印重复项

问题描述 投票:0回答:1

我是编程新手,但正在掌握它的窍门。但我一直试图从字典列表中打印重复项,这给了我以下错误:
My Error Output

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}) which 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);
 
        }
    }
}
c# list dictionary duplicates
1个回答
0
投票

您可以使用

SelectMany
来展平所有字典中的键值对:

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

您当前正在按词典本身进行分组。这不起作用,因为

Dictionary<tkey,tval>
不会覆盖
Equals
+
GetHashCode
。您也不想查找重复的字典,而是查找重复的键。

© www.soinside.com 2019 - 2024. All rights reserved.