如何初始化包含字典列表的字典? [英] How to initialize a dictionary containing lists of dictionaries?
问题描述
我想创建一个字典,其中包含以下使用Generic Collections的字典列表:
{alfred,[{age 20.0}。 {height_cm,180.1}],
barbara,[{age,18.5},{height_cm,167.3}],
chris,[{age },{height_cm,179.0}]
}
我从以下开始:
使用System.Collections.Generic;
字典< String,Dictionary< String,double> []>者;
但是,我想从上面将三个记录一次性插入到人员中。我一直困扰着语法错误。
任何人都有解决方案吗?
编辑:
谢谢大家 - 我没想到在这么短的时间内会收到很多很好的答案!你很棒!
您可以使用字典初始化。不如Python那么优雅,但可以与以下方式一起使用:
var persons = new Dictionary< string,Dictionary< string,double>> ;
{
{alfred,新词典< string,double> {{age,20.0},{height_cm,180.1}}},
{barbara,新词典< string,double> {{age,18.5},{height_cm,167.3}}},
{chris,新词典< string,double> {{age,39.0},{height_cm,179.0}}}
};
然后:
人[ 阿尔弗雷德] [ 年龄];
另请注意,您需要 Dictionary< string,Dictionary< string,double> < / code>,而不是
字典< string,Dictionary< string,double> []>
。
同样使用这样的结构可能会有一点PITA,损害可读性和编译时类型的安全性。
在.NET中,喜欢使用强类型的对象,如下所示:
public class Person
{
public double Age {get;组; }
public string Name {get;组; }
public double HeightCm {get;组;
}
然后:
var persons = new []
{
new Person {Name =alfred,Age = 20.0,HeightCm = 180.1},
new Person {Name =barbara,Age = 18.5,HeightCm = 180.1},
new Person {Name =chris,Age = 39.0,HeightCm = 179.0},
};
然后您可以使用LINQ获取所需的任何信息:
double barbarasAge =
(from p in persons
其中p.Name ==barbara
select p.Age) 。第一();当然,使用集合不会像哈希表查找一样快,但是取决于你的您可以根据自己的表现来满足需求。
I am starting to do a little development in C#, and I am stuck with a problem here. Usually I develop in Python where stuff like this is being implemented easily (at least for me), but I have no idea how to do that in C#:
I want to create a dictionary containing a list of dictionaries like the following using Generic Collections:
{ "alfred", [ {"age", 20.0}. {"height_cm", 180.1} ],
"barbara", [ {"age", 18.5}, {"height_cm", 167.3} ],
"chris", [ {"age", 39.0}, {"height_cm", 179.0} ]
}
I started with the following:
using System.Collections.Generic;
Dictionary<String, Dictionary<String, double>[]> persons;
But then I'd like to insert the three records from above at once into persons. I am stuck with syntax errors all the way.
Anyone have a solution for me?
Edit:
Thank you all - I didn't expect to receive so many well thought answers in such a short time! You are great!
解决方案 You could use dictionary initializes. Not as elegant as Python, but could live with:
var persons = new Dictionary<string, Dictionary<string, double>>
{
{ "alfred", new Dictionary<string, double> { { "age", 20.0 }, { "height_cm", 180.1 } } },
{ "barbara", new Dictionary<string, double> { { "age", 18.5 }, { "height_cm", 167.3 } } },
{ "chris", new Dictionary<string, double> { { "age", 39.0 }, { "height_cm", 179.0 } } }
};
And then:
persons["alfred"]["age"];
Also notice that you need Dictionary<string, Dictionary<string, double>>
for this structure and not Dictionary<string, Dictionary<string, double>[]>
.
Also working with such structure could be a little PITA and harm readability and compile-time type safety of the code.
In .NET it is preferred to work with strongly typed objects, like this:
public class Person
{
public double Age { get; set; }
public string Name { get; set; }
public double HeightCm { get; set; }
}
and then:
var persons = new[]
{
new Person { Name = "alfred", Age = 20.0, HeightCm = 180.1 },
new Person { Name = "barbara", Age = 18.5, HeightCm = 180.1 },
new Person { Name = "chris", Age = 39.0, HeightCm = 179.0 },
};
and then you could use LINQ to fetch whatever information you need:
double barbarasAge =
(from p in persons
where p.Name == "barbara"
select p.Age).First();
To be noted of course that using collections would not be as fast as a hashtable lookup but depending on your needs in terms of performance you could also live with that.
这篇关于如何初始化包含字典列表的字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!