dictionary

How to use a dot "." to access members of dictionary?

How do I make Python dictionary members accessible via a dot "."? For example, instead of writing mydict['val'], I'd like to write mydict.val. Also I'd like to access nested dicts this way. For example, mydict.mydict2.val would refer to mydict = { 'mydict2': { 'val': ... } }. Thanks, Boda Cydo. ...

The difference between python dict and tr1::unordered_map in C++

I have a question related to understanding of how python dictionaries work. I remember reading somewhere strings in python are immutable to allow hashing, and it is the same reason why one cannot directly use lists as keys, i.e. the lists are mutable (by supporting .append) and hence they cannot be used as dictionary keys. I wanted to...

Is it possible to group a LINQ set into a dictionary with one query?

Given a table of order items (OrderItems) that relates to a table of orders (Orders), which in turn relates to a table of users (Users), is it possible to retrieve all the OrderItems and group them into a dictionary by OrderId with just one query? ie. without performing an iteration over either the OrderItems result set or performing a q...

What is the fastest dictionary based class implementation?

Which one of the C# <Key, Value> structure implementations has better performance and higher speed? P.S.1: I Have two thread, one of them writes into collection, and another one reads and writes into it. P.S.2: Key items are random numbers, and then my access is random. Write and read actions are simultaneous. I am using hashtable, but...

Eficient way to remove all entries in a dictionary lower than a specified value

Hi everyone, so i need to remove all entries in a dictionary accordingly to a specified lower bound. My current solution is this: List<string> keys = new List<string>(); foreach (KeyValuePair<string, int> kvp in dic) { if (kvp.Value < lowerBound) keys.Add(kvp.Key); } foreach (string key in key...

Is it possible to create a static dictionary in a C++/CLI environment?

I used in my C++/CLI project static array's like: static array < array< String^>^>^ myarray= { {"StringA"}, {"StringB"} }; Is it possible to create a dictionary the same way? I was not able to create and initialize one. static Dictionary< String^, String^>^ myDic= { {"StringA", "1"}, {"StringB", "2"} }; Thx ...

merging Python dictionaries

Hi everyone, I am trying to merge the following python dictionaries as follow: dict1= {'paul':100, 'john':80, 'ted':34, 'herve':10} dict2 = {'paul':'a', 'john':'b', 'ted':'c', 'peter':'d'} output = {'paul':[100,'a'], 'john':[80, 'b'], 'ted':[34,'c'], 'peter':[None, 'd'], 'herve':[10, None]} Any tip about an efficient way to do this?...

Hashtable/Dictionary but with key composed of multiple values?

Lets say I have an object that has stringProp1, stringProp2. I wish to store each combination of stringProp1, stringProp2 in a Dictionary. Initially I was storing the key as key = stringProp1+stringProp2 but this can actually cause a bug depending on the 2 values. Is the best solution for this problem to create a custom dictionary cla...

Creating a Generic Dictionary From a Generic List

I am trying to do the following but I think I must be missing something...(fairly new to generics) (Need to target .NET 2.0 BTW) interface IHasKey { string LookupKey { get; set; } } ... public static Dictionary<string, T> ConvertToDictionary(IList<T> myList) where T : IHasKey { Dictionary<string, T> dict = new Dictionary<stri...

Unexpected issue Copying Dictionaries

My idea was to copy a dictionary while resetting all the values of the previous one, so i have this instruction: var dic2 = new Dictionary<string, int>(dic.ToDictionary(kvp => kvp.Key, kvp => 0)); However i had an unexpected problem doing this, since the new copied dictionary doesnt have the same order of keys of the previous one....

Is there a way to keep track of the ordering of items in a dictionary?

I have a Dictionary<Guid, ElementViewModel>. (ElementViewModel is our own complex type.) I add items to the dictionary with a stock standard items.Add(Guid.NewGuid, new ElementViewModel() { /*setters go here*/ });, At a later stage I remove some or all of these items. A simplistic view of my ElementViewModel is this: class ElementVi...

What's the easiest way to get Dictionary functionality in VB.NET?

I want to statically define a mapped array of strings like: var dict = {cat:50, bat:10, rat:30}; and lookup values within it like: MessageBox.Show( dict["cat"] ) ...

Deserialization problem with Dictionary.

Hi when i am trying to serialize a dictionary, everything worked fine. but when i am deserializing it is showing count as 0. But, it works fine with a list. What is goingon exactly when we are deserializing a list and a dictionary? ...

Trouble with an enumeration as a key in a Dictionary collection

I have a scenario where I'm using a Dictionary to hold a list of transaction types that a certain system accepts. The key in the Dictionary is an enum field, the value is an int. At some point in the system, we're going to want to do something like this: sqlCommand.Parameters.AddWithValue("@param", LookupDictionary[argument.enumField])...

Get size of ActionScript 3 Dictionary

var d:Dictionary = new Dictionary(); d["a"] = "b"; d["b"] = "z"; How to get the length/size of the dictionary (which is 2) ? ...

Can anyone explain why Dictionary<> in C# doesn't work like map<T,U> in STL?

When I first started to program in C# last year, I immediately looked for the equivalent to STL's map, and learned about Dictionary. UPDATE crossed out this garbage below, I was completely wrong. My experience with STL's map was that I hated when I requested it for a value, and if the key wasn't in the map, it would automatically creat...

python: inheriting or composition

Let's say that I have class, that uses some functionality of dict. I used to composite a dict object inside and provide some access from the outside, but recently thought about simply inheriting dict and adding some attributes and methods that I might require. Is it a good way to go, or should I stick to composition? ...

Having trouble getting NUnit's Assert.Throws to work properly

I could have sworn that I've used NUnit's Assert.Throws to determine whether or not a particular exception gets thrown from a method, but my memory has failed me before. I read this post here on SO, but it didn't answer my question, as I know the correct syntax, and I don't want to do anything with the exception that gets returned (I do...

How to dynamcially call a generic method based on a mapping in a dictionary?

I have a method String Foo<T> where T: WebControl Now I do have a string like "hyperlink". What is want is to call Foo<Hyperlink> based on a mapping from the string to the generic. How does the dictionary have to look like? It ain't: private Dictionary<string, Type> _mapping = new Dictionary<string, Type>() { {"hyperlink", ty...

How to properly subclass dict and override get/set

I am debugging some code and I want to find out when a particular dictionary is accessed. Well, it's actually a class that subclass dict and implements a couple extra features. Anyway, what I would like to do is subclass dict myself and add override __getitem__ and __setitem__ to produce some debugging output. Right now, I have class Di...