转换结构的算法(传递闭包?)

发布于 2024-12-04 11:04:20 字数 1479 浏览 1 评论 0 原文

我的源结构要么是一个字典,如下所示:

new Dictionary<string, string>();
dic.Add("Dinges", new List<String>() { "A", "B" });
dic.Add("Facebook", new List<String>() { "Dinges" });
dic.Add("SocialMedia", new List<String>() { "FaceBook" });
dic.Add("Medium", new List<String>() { "SocialMedia" })

要么是一个元组列表,如下所示:

new List<Tuple<String, String>>();
list.Add(Tuple.Create("Dinges", "A");
list.Add(Tuple.Create("Dinges", "B");
list.Add(Tuple.Create("Facebook", "Dinges"); 
list.Add(Tuple.Create("SocialMedia", "Facebook");
list.Add(Tuple.Create("Medium", "SocialMedia");

这些是项目之间的关联,也许最好将其描述为继承树。 Medium 是最通用的类​​别,A 是最专业的类别。

我正在寻找的是一种重新排列项目的方法,如下所示:

new List<Tuple<String, String>>();
list.Add(Tuple.Create("Dinges", "A");
list.Add(Tuple.Create("Dinges", "B");
list.Add(Tuple.Create("Facebook", "Dinges"); 
list.Add(Tuple.Create("Facebook", "A");
list.Add(Tuple.Create("Facebook", "B");
list.Add(Tuple.Create("SocialMedia", "A");
list.Add(Tuple.Create("SocialMedia", "B");
list.Add(Tuple.Create("SocialMedia", "Dinges");
list.Add(Tuple.Create("SocialMedia", "Facebook");
list.Add(Tuple.Create("Medium", "A");
list.Add(Tuple.Create("Medium", "B");
list.Add(Tuple.Create("Medium", "Dinges");
list.Add(Tuple.Create("Medium", "FaceBook");
list.Add(Tuple.Create("Medium", "SocialMedia");

我确实需要一些帮助来完成此任务,我在传递闭包方面有一些技巧,但我真的无法理解围绕它。任何帮助将非常感激。

My source structure is either a Dictionary looking like:

new Dictionary<string, string>();
dic.Add("Dinges", new List<String>() { "A", "B" });
dic.Add("Facebook", new List<String>() { "Dinges" });
dic.Add("SocialMedia", new List<String>() { "FaceBook" });
dic.Add("Medium", new List<String>() { "SocialMedia" })

Or a list of tuples looking like:

new List<Tuple<String, String>>();
list.Add(Tuple.Create("Dinges", "A");
list.Add(Tuple.Create("Dinges", "B");
list.Add(Tuple.Create("Facebook", "Dinges"); 
list.Add(Tuple.Create("SocialMedia", "Facebook");
list.Add(Tuple.Create("Medium", "SocialMedia");

These are associations between items, maybe best described as an inheritance tree. Medium being the most general class, A the most specialized.

What I'm looking for is a way to rearrange the items to look like the following:

new List<Tuple<String, String>>();
list.Add(Tuple.Create("Dinges", "A");
list.Add(Tuple.Create("Dinges", "B");
list.Add(Tuple.Create("Facebook", "Dinges"); 
list.Add(Tuple.Create("Facebook", "A");
list.Add(Tuple.Create("Facebook", "B");
list.Add(Tuple.Create("SocialMedia", "A");
list.Add(Tuple.Create("SocialMedia", "B");
list.Add(Tuple.Create("SocialMedia", "Dinges");
list.Add(Tuple.Create("SocialMedia", "Facebook");
list.Add(Tuple.Create("Medium", "A");
list.Add(Tuple.Create("Medium", "B");
list.Add(Tuple.Create("Medium", "Dinges");
list.Add(Tuple.Create("Medium", "FaceBook");
list.Add(Tuple.Create("Medium", "SocialMedia");

I really need some help accomplishing this, I've had some tips in the way of a transitive closure, but I really can;t wrap my head around it. Any help would be really much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一向肩并 2024-12-11 11:04:20

从字典方法来看,我认为应该将其声明为 Dictionary>

private static IEnumerable<string> TransitiveValues(string name,
                    Dictionary<string, List<string>> lookup)
{
    yield return name;
    List<string> children;
    if (lookup.TryGetValue(name, out children))
    {
        foreach (string child in children)
        {
            foreach (string value in TransitiveValues(child, lookup))
            {
                yield return value;
            }
        }
    }
}

然后:

var query = from name in dictionary.Keys
            from value in TransitiveValues(name, dictionary)
            select Tuple.Create(name, value);
var list = query.ToList();

只需确保没有任何循环:)

From the dictionary approach, which I assume was meant to be declared as a Dictionary<string, List<string>>:

private static IEnumerable<string> TransitiveValues(string name,
                    Dictionary<string, List<string>> lookup)
{
    yield return name;
    List<string> children;
    if (lookup.TryGetValue(name, out children))
    {
        foreach (string child in children)
        {
            foreach (string value in TransitiveValues(child, lookup))
            {
                yield return value;
            }
        }
    }
}

Then:

var query = from name in dictionary.Keys
            from value in TransitiveValues(name, dictionary)
            select Tuple.Create(name, value);
var list = query.ToList();

Just make sure you don't have any cycles :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文