dictionary

Where Do I Store Hash Table or Dictionary Key Names

When I'm working with Hash Tables/Dictionaries I sometimes struggle with how to specify keys. For example: if I create a simple Dictionary (using Python for this example), foo = {'bar': 'baz', 'foobar': 'foobaz' } I can access values (in other modules) with the key values: (foo['bar']) and get baz back. In the words of Dr. Evil, "pr...

Using NameValueCollection in C# webservice gives not XML serializable error

I am getting this error message when I try to "Add Web Reference" to my ASMX proxy project: **"To be XML serializable, types which inherit from ICollection must have an implementation of Add(System.String) at all levels of their inheritance hierarchy. System.Collections.Specialized.NameValueCollection does not implement Add(System.St...

Selecting a good dictionary key

I have an object that I want to use to look up other objects. I will be using a Dictionary<TKey, TValue>(). The key object has two strings that uniquely identify it, say KeyObj.Str1 and KeyObj.Str2. What do you recommend that I use as the key for the dictionary? 1: The concatenation of the strings. Dictionary<String, TValue>(); Key...

Memory efficiency: One large dictionary or a dictionary of smaller dictionaries?

I'm writing an application in Python (2.6) that requires me to use a dictionary as a data store. I am curious as to whether or not it is more memory efficient to have one large dictionary, or to break that down into many (much) smaller dictionaries, then have an "index" dictionary that contains a reference to all the smaller dictionarie...

Dictionary enumeration in c#

Hi, I want to enumerate dictionary. How i enumerate. Thanks ...

Is there a read-only generic dictionary available in .NET?

I'm returning a reference to a dictionary in my read only property. How do I prevent consumers from changing my data? If this were an IList I could simply return it AsReadOnly. Is there something similar I can do with a dictionary? Private _mydictionary As Dictionary(Of String, String) Public ReadOnly Property MyDictionary() As Dicti...

Python: How to extract variable name of a dictionary entry?

I'm wondering how I would go about finding the variable name of a dictionary element: For example: >>>dict1={} >>>dict2={} >>>dict1['0001']='0002' >>>dict2['nth_dict_item']=dict1 >>>print dict2 {'nth_dict_item': {'0001': '0002'}} >>>print dict2['nth_dict_item'] {'001': '002'} How can I go about making...

Java Class that implements Map and keeps insertion order?

I'm looking for a class in java that has key-value association, but without using hashes. Here is what I'm currently doing: Add values to a Hashtable Get an iterator for the Hashtable.entrySet(). Iterate through all values and: Get a Map.Entry for the iterator Create an object of type Module (a custom class) based on the value. Add t...

How to create a property class in c#?

I want to create a class which will have two properties, e.g. key & value. And I want one method which will give me a value based on the key. So what is the code? I know Hashtable but how to implement it in C#? Can I have a string as a key? ...

Static Generic Class as Dictionary

A static field in a generic class will have a separate value for each combination of generic parameters. It can therefore be used as a Dictionary<Type, whatever> Is this better or worse than a static Dictionary<Type, whatever>? In other words, which of these implementations more efficient? public static class MethodGen<TParam> { ...

Building a dictionary of counts of items in a list

I have a List containing a bunch of strings that can occur more than once. I would like to take this list and build a dictionary of the list items as the key and the count of their occurrences as the value. Example: List<string> stuff = new List<string>(); stuff.Add( "Peanut Butter" ); stuff.Add( "Jam" ); stuff.Add( "Food" ); stuff.Ad...

Hashtable with MultiDimensional Key in C#

I'm basically looking for a way to access a hashtable value using a two-dimensional typed key in c#. Eventually I would be able to do something like this HashTable[1][false] = 5; int a = HashTable[1][false]; //a = 5 This is what I've been trying...hasn't worked Hashtable test = new Hashtable(); test.Add(new Dictionary<int, bool>() ...

Which collection for storing unique strings?

I'm looking for a collection just like Dictionary(OF Key, Value) but I don't actually need a key and value. Key itself is enough. So something like Collection(Key). It shouldn't accept duplicate keys. I've looked up couple of collections in .NET Framework but couldn't find what I want. Currently I'm abusing Dictionary(OF String, String)...

ContainsKey Thread Safe

In the following code: public class StringCache { private readonly object lockobj = new object(); private readonly Dictionary<int, string> cache = new Dictionary<int, string>(); public string GetMemberInfo(int key) { if (cache.ContainsKey(key)) return cache[key]; lock (lockobj) { ...

How to work around needing to update a dictionary

I need to delete a k/v pair from a dictionary in a loop. After getting RuntimeError: dictionary changed size during iteration I pickled the dictionary after deleting the k/v and in one of the outer loops I try to reopen the newly pickled/updated dictionary. However, as many of you will probably know-I get the same error-I think when it...

python dict update diff

Does python have any sort of built in functionality of notifying what dictionary elements changed upon dict update? For example I am looking for some functionality like this: >>> a = {'a':'hamburger', 'b':'fries', 'c':'coke'} >>> b = {'b':'fries', 'c':'pepsi', 'd':'ice cream'} >>> a.diff(b) {'c':'pepsi', 'd':'ice cream'} >>> a.update(b...

C# dictionary<> missing key

When i do val = dict["nonexistent key"] i get System.Collections.Generic.KeyNotFoundException Is there a way i have my dictionary call a member function with the key as a param to generate a value? -edit- Maybe i should of been more specific. I want to AUTOMATICALLY call a member function to do what it needs create the proper value for ...

What is the best ordered dict implementation in python?

I've seen (and written) a number of implementations of this. Is there one that is considered the best or is emerging as a standard? What I mean by ordered dict is that the object has some concept of the order of the keys in it, similar to an array in PHP. odict from PEP 372 seems like a strong candidate, but it's not totally clear tha...

Why should the "key" part in a JS hash/dict be a string?

In most JSON serializers/deserializers, the "key" part in a javascript dictionary/hash array is written as a string. What is the benefit of using a string as the key as opposed to just typing the intended name in? For example, say I define two objects k1 and k2 like so: var k1 = { a: 1, b: 2, c: 3 }; // define name normally v...

Using class as key in NSDictionary.

I'm writing a contextual "factory" that will maintain a dictionary of converter/acting objects which inherit from some Converter class. This class has a method: - (Class)classResponsibility Or something similar, such that a StringConverter class would implement the method as: - (Class)classResponsibility { return [NSString class]...