【发布时间】: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