如何构造Dictionary <string,list <string =“”>&gt;的实例

时间:2018-04-16 14:53:08

标签: c# object-initializers

我正在尝试使用C#进行编码,但我发现我无法像C ++那样构建实例:

Dictionary<string, List<string>> FirSet =
    new Dictionary<string, List<string>>() {
        { "a", {"ab", "abc"} },
        { "b", {"bc", "bcd"} }
    };

2 个答案:

答案 0 :(得分:6)

您必须使用new List<string>()

启动词典中的列表

这是怎么做的:

Dictionary<string, List<string>> FirSet = new Dictionary<string, List<string>>()
{
    { "a", new List<string> { "ab", "abc" }},
    { "b", new List<string> { "bc", "bcd" }}
};

答案 1 :(得分:2)

def similar(x, rel_std=0.001):
    sigma = rel_std * x
    mu = x
    return sigma * np.random.randn(*x.shape) + mu

您还可以使用C#6.0语法:

    var firSet =
        new Dictionary<string, List<string>>()
        {
            { "a", new List<string>() {"ab", "abc"} },
            { "b", new List<string>() {"bc", "bcd"} }
        };

如果你觉得特别懒,而且你不想输入所有这些 var firSet = new Dictionary<string, List<string>>() { ["a"] = new List<string> {"ab", "abc"}, ["b"] = new List<string> {"bc", "bcd"} }; ,你可以随时这样做:

new List<string>