如何打印字典? [英] How do I print a dictionary?

查看:177
本文介绍了如何打印字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一本字典,但它有点复杂(即字典<字符串,字典<字符串,Hashset<字符串>>。如何打印像这样复杂的东西?

So I have a dictionary but it's a bit complex (i.e Dictionary < string, Dictionary < string , Hashset < string > > . How can I print something as complex as this?

推荐答案

上下文:Andreas(上图)向OP询问了他提出的数据结构的异国情调性质.OP回复说:我知道这真的很糟糕,而且我正在努力抽象它有点因为它真的难以使用。



尽管我能理解它,OP正在使用这种数据结构:
Context: Andreas (above) asked the OP about the "exotic" nature of his proposed data-structure. The OP replied: "I know it's really bad, and I'm trying to abstract it a bit because it is really hard to work with."

As best as I can understand it, the OP is using this data-structure:
Dictionary<
    string,
    HashSet<
        Dictionary<
            string,
            HashSet<string>>>>

我认为尝试通过考虑如何改进这样的数据结构是有用的map到类:



我首先定义来自outermos的嵌套数据元素最内层:

I think it's useful to try and approach improving a data-structure like this by considering how it would "map" to Classes:

I'd start by defining the nested data-elements from "outermost to innermost:"

// outer Dictionary<string,>
public class OuterD : Dictionary<string, InnerH>
{
    public static StringBuilder sb = new StringBuilder();

    public override string ToString()
    {
        sb.Clear();
        sb.AppendLine("Outer Dictionary:");
        
        foreach (var kvp in this)
        {
            sb.Append("\tKey: ");
            sb.AppendLine(kvp.Key);
            sb.Append("\tValue: ");
            sb.Append(kvp.Value.ToString());
            sb.AppendLine();
        }

        return sb.ToString();
    }
}

// the HashSet which is the Value of the outermost Dictionary
public class InnerH : HashSet<InnerD>
{
    public override string ToString()
    {
        // left for you to write: use the StringBuilder like this:
        OuterD.sb.AppendLine("\t\tHashSet of Outer Dictionary Value Value:");

        foreach (var hshval in this)
        {
            // left for you to write
        }

        return "";
    }
}

// the innermost Dictionary used as elements of the HashSet
public class InnerD : Dictionary<string,HashSet<string>>
{
    public override string ToString()
    {
        // left for you to write: use the StringBuilder like this:
        OuterD.sb.AppendLine("\t\t\tInner Dictionary of Inner HashSet:");

        foreach (var kvp in this)
        {
            OuterD.sb.Append("\t\t\tKey: ");
            OuterD.sb.AppendLine(kvp.Key);
            
            foreach (var hsh in kvp.Value)
            {
                // for you to write
            }
        }

        return "";
    } 
}

从这个观点看数据结构的跳出来是你有两个级别哈希在这里进行:为什么你需要两个;为什么你不能只使用字典< string,string>对于最里面的词典,只有Hash the Dictionary本身?



我希望你能看到谁打印的任务...如果通过打印你的意思是渲染将数据结构的当前状态转换为一个字符串...可以通过在每个类中重载'ToString()然后基本上链接它们的调用来完成,同时提供用于保存汇编字符串的'静态StringBuilder。

What "jumps out" at me looking at the data-structure from this "point-of-view" is that you've got two "levels" of hashing going on here: why do you need two; why can't you just use a Dictionary<string, string> for the innermost Dictionary, and only Hash the Dictionary itself ?

I hope you can see who the task of printing ... if by "printing" you mean rendering the current state of the data-structure into one string ... can be done by overloading 'ToString() in each of the Classes and then essentially "chaining" their calls, while providing a 'static StringBuilder to use to hold the assembling string.


你所拥有的是一个多维数据结构。

创建一个表格转储,每列代表一个维度。

In你的情况(假设你的数据结构是变量 data ):
What you have is a multi-dimensional data structure.
Create a tabular dump, each column represents one dimension.
In your case (assuming your data structure is in variable data):
foreach(var dataKey in data.Keys)
{
    var dataValue = data[dataKey]; // HashSet
    foreach(var dataValueValue in dataValue) // Dictionary
    {
        foreach(var dataValueValueKey in dataValueValue.Keys)
        {
            var dataValueValueValue = dataValueValue[dataValueValueKey]; // HashSet
            foreach(var dataValueValueValueValue in dataValueValueValue) // string
            {
                 Console.WriteLine("{0};{1};{2};{3};{4}"
                                  , dataKey
                                  , dataValue.GetHashCode()
                                  , dataValueValueKey
                                  , dataValueValueValue.GetHashCode()
                                  , dataValueValueValueValue
                                  );
            }
        }
    }
}

在某些CSV查看器中打开它,例如MS Excel。

干杯

Andi

PS:而不是笨拙的数据...价值/数据...关键变量给他们一些逻辑名称。

Open it in some CSV viewer, e.g. MS Excel.
Cheers
Andi
PS: Instead of the awkward data...Value/data...Key variables give them some logical names.


这篇关于如何打印字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆