Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a dictionary containing lists of dictionaries?

Tags:

c#

dictionary

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!

like image 546
mawimawi Avatar asked Sep 18 '11 09:09

mawimawi


People also ask

How do you initialise a dictionary?

Another way to initialize a python dictionary is to use its built-in “dict()” function in the code. So, you have to declare a variable and assign it the “dict()” function as an input value. After this, the same print function is here to print out the initialized dictionary.

Can a dictionary have a list as a value?

It definitely can have a list and any object as value but the dictionary cannot have a list as key because the list is mutable data structure and keys cannot be mutable else of what use are they.

How do you create a dictionary in a list of values?

The easiest way to create a dictionary from a list of keys and values is to apply the dict() function and inside the function take the zip() method as an argument.


1 Answers

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.

like image 79
Darin Dimitrov Avatar answered Oct 07 '22 08:10

Darin Dimitrov