Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何比较一个字典的值?_C#_Dictionary - Fatal编程技术网

C# 如何比较一个字典的值?

C# 如何比较一个字典的值?,c#,dictionary,C#,Dictionary,因此,我有一个字典,每个字符串键可以接受许多值,这些值被分为2个哈希集(即Dictionary

因此,我有一个字典,每个字符串键可以接受许多值,这些值被分为2个哈希集(即Dictionary,HashSet>)

现在我把字典填成这样

Key Values1 Values2 A Z B C D E movie0 movie1 movie2 movie7 B A C D E movie1 movie2 movie7 C A B D movie1 movie7 ... ..... ....etc 关键值1值2 A Z B C D E电影0电影1电影2电影7 B A C D E电影1电影2电影7 C A B D电影1电影7 ... ..... ....等
现在我需要做一个循环值2的函数,这个函数将使用2个字符串键(即a,B),如果值匹配,它将增加一个计数器,因此在a和B的情况下,它们都出现在电影1,电影2和电影7中,所以计数器将返回3。有没有办法做到这一点?

您的数据结构看起来很奇怪。顺便说一句,如果你想这样做

int count = dic["string1"].Item2.Intersect(dic["string2"].Item2).Count();
试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary < string, Tuple < HashSet < string >, HashSet < string>>> dict = new Dictionary<string,Tuple<HashSet<string>,HashSet<string>>>() {
                { "A", new Tuple<HashSet<string>, HashSet<string>>( new HashSet<string>() {"Z", "B", "C", "D", "E"}, new HashSet<string>() { "movie0",  "movie1", "movie2", "movie7"})},
                { "B", new Tuple<HashSet<string>, HashSet<string>>( new HashSet<string>() {"A", "C", "D", "E"}, new HashSet<string>() { "movie1",  "movie2", "movie7"})},
                { "C", new Tuple<HashSet<string>, HashSet<string>>( new HashSet<string>() {"A", "B", "D"}, new HashSet<string>() { "movie1",  "movie7"})}
            };

            int mathces = Matches(dict["A"].Item2, dict["B"].Item2);

        }
        static int Matches(HashSet<string> hash1, HashSet<string> hash2)
        {
            return hash1.Where(x => hash2.Contains(x)).Count();
        }
    }
}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
Dictionary,HashSet>dict=newdictionary(){
{“A”,新的元组(新的HashSet(){“Z”,“B”,“C”,“D”,“E”},新的HashSet(){“movie0”,“movie1”,“movie2”,“movie7”}),
{“B”,新的元组(新的HashSet(){“A”,“C”,“D”,“E”},新的HashSet(){“movie1”,“movie2”,“movie7”}),
{“C”,新元组(新HashSet(){“A”,“B”,“D”},新HashSet(){“movie1”,“movie7”})
};
int mathces=匹配项(dict[“A”].Item2,dict[“B”].Item2);
}
静态int匹配(HashSet hash1、HashSet hash2)
{
返回hash1.Where(x=>hash2.Contains(x)).Count();
}
}
}
​

您需要接受面向对象编程。把所有的东西分成几类,然后用作文。你在折磨字典和字符串类。嗯,我本来打算制作两个字典,一个字符串和一个哈希集,但我认为这很糟糕。。。这是一个更好的开始吗?你会发现你的程序几乎可以做任何事情。您可以这样做,但它有一些折衷,例如可读性、可维护性或可扩展性。像@roryap一样,我建议稍微分解一下您的数据结构,或者澄清为什么需要以这种方式循环。看起来你的数据太复杂了,无法用一个简单的解决方案。嗯,我的数据结构应该是一个图形,我填写字典的方式是从一个文件中读取。。。我的应用程序应该在3个不同的函数中演示小世界问题,第一个是计算2个节点之间的分离度(即A/B),我实际上能够通过使用BFS来演示它,并且只制作类似于此的数据结构(即字典)所以电影并不是很重要,但现在我需要计算每个节点出现的电影数量,所以我想在电影和节点及其邻居之间建立一种连接的方式,所以是的,这就是为什么我想到tuple,这就是为什么我需要以这种方式循环。。。抱歉,我希望这能回答你关于为什么我需要这样循环的问题。谢谢你,这很有效,但我想我必须想一个更好的方式来表达我的数据结构!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary < string, Tuple < HashSet < string >, HashSet < string>>> dict = new Dictionary<string,Tuple<HashSet<string>,HashSet<string>>>() {
                { "A", new Tuple<HashSet<string>, HashSet<string>>( new HashSet<string>() {"Z", "B", "C", "D", "E"}, new HashSet<string>() { "movie0",  "movie1", "movie2", "movie7"})},
                { "B", new Tuple<HashSet<string>, HashSet<string>>( new HashSet<string>() {"A", "C", "D", "E"}, new HashSet<string>() { "movie1",  "movie2", "movie7"})},
                { "C", new Tuple<HashSet<string>, HashSet<string>>( new HashSet<string>() {"A", "B", "D"}, new HashSet<string>() { "movie1",  "movie7"})}
            };

            int mathces = Matches(dict["A"].Item2, dict["B"].Item2);

        }
        static int Matches(HashSet<string> hash1, HashSet<string> hash2)
        {
            return hash1.Where(x => hash2.Contains(x)).Count();
        }
    }
}
​