dictionary

Creating a constant Dictionary in C#

What is the most efficient way to create a constant (never changes at runtime) mapping of strings to ints? I've tried using a const Dictionary, but that didn't work out. I could implement a immutable wrapper with appropriate semantics, but that still doesn't seem totally right. For those who have asked, I'm implementing IDataError...

Getting key with maximum value in dictionary?

I have a dictionary: keys are strings, values are integers. Example: stats = {'a':1000, 'b':3000, 'c': 100}. I'd like to get 'b' as an answer, since it's the key with a higher value. I did the following, using an intermediate list with reversed key-value tuples: inverse = [(value, key) for key, value in stats.items()] print ma...

Replacing multiple occurrences in nested arrays

I've got this python dictionary "mydict", containing arrays, here's what it looks like : mydict = dict( one=['foo', 'bar', 'foobar', 'barfoo', 'example'], two=['bar', 'example', 'foobar'], three=['foo', 'example']) i'd like to replace all the occurrences of "example" by "someotherword". While I can already think of a f...

Add container class to dictionary

I have a Dictionary that when I add multiple values to it, the items that were entered before take the values of the item added. I am using the .Net 3.5 Here is the code: public static Dictionary<string, Neighborhoods> Families() { if (File.Exists(calculatePath() + "Family.txt")){} else {File.Create(calculatePath() +...

Does .NET have a Dictionary implementation that is equivalent to Java's ConcurrentHashMap?

To recap for those .NET gurus who might not know the Java API: ConcurrentHashMap in Java has atomic methods (i.e. require no external locking) for common Map modification operations such as: putIfAbsent(K key, V value) remove(Object key, Object value) replace(K key, V value) It also allows iteration over the keyset without locking (i...

How to get a ReadOnlyCollection<T> of the Keys in a Dictionary<T, S>

My class contains a Dictionary<T, S> dict, and I want to expose a ReadOnlyCollection<T> of the keys. How can I do this without copying the Dictionary<T, S>.KeyCollection dict.Keys to an array and then exposing the array as a ReadOnlyCollection? I want the ReadOnlyCollection to be a proper wrapper, ie. to reflect changes in the underl...

Merging dictionaries in C#

What's the best way to merge 2 or more dictionaries (Dictionary<T1,T2>) in C#? (3.0 features like LINQ are fine). I'm thinking of a method signature along the lines of: public static Dictionary<TKey,TValue> Merge<TKey,TValue>(Dictionary<TKey,TValue>[] dictionaries); or public static Dictionary<TKey,TValue> ...

Word macro accessing contents of custom dictionary?

I need to create a way of checking various codes in a Word document. These codes are usually a letter followed by a hyphen and 5 digits, eg M-81406. We need to check that these pre-defined codes have been typed in correctly (there is a pre-determined list of several thousand codes). We cannot use normal Word spell checking as you cannot ...

Built-in List that can be accessed by index and key

Is it possible to create a list that can be access by either an index or a key? I am looking for a Collection type that already exists but has this facility, I want to avoid redefining the indexers ...

Python - Create a list with initial capacity

Code like this often happens: l = [] while foo: #baz l.append(bar) #qux This is really slow if you're about to append thousands of elements to your list, as the list will have to constantly be re-initialized to grow. (I understand that lists aren't just wrappers around some array-type-thing, but something more complicated....

Declare a Dictionary inside a static class

How to declare a static dictionary object inside a static class? I tried public static class ErrorCode { public const IDictionary<string , string > ErrorCodeDic =new Dictionary<string, string>() { {"1","User name or password problem"} }; } But the compiler complains that "A const field of a re...

Is it possible to initialise a New System.Collections.Generic.Dictionary with String key/value pairs?

Is it possible to create and initialise a System.Collections.Generic.Dictionary object with String key/value pairs in one statement? I'm thinking along the lines of the constructor for an array of Strings.. e.g. Private mStringArray As String() = {"String1", "String2", "etc"} In case this is turns out to be a syntactic sugar kind of...

What is the equivalent of map<int, vector<int> > in Python?

In C++ often do something like this: typedef map<int, vector<int> > MyIndexType; Where I then use it like this: MyIndexType myIndex; for( ... some loop ...) { myIndex[someId].push_back(someVal); } If there was no entry in the map the code will insert a new empty vector and then append to it. In Python it would look like this: m...

How do I declare a TDictionary enumerator?

I've got a TDictionary that stores a bunch of objects indexed by name, and I'd like to be able to examine all of the objects. So I tried this: var enumerator: TMyObject; begin for enumerator in myDictionary do But that wouldn't compile. "Incompatible types: 'TMyObject' and 'TPair' So I tried it a bit differently: var enumerator:...

Hopefully simple question about modifying dictionaries in C#

I have a huge dictionary of blank values in a variable called current like so: struct movieuser {blah blah blah} Dictionary<movieuser, float> questions = new Dictionary<movieuser, float>(); So I am looping through this dictionary and need to fill in the "answers", like so: for(var k = questions.Keys.GetEnumerator();k.MoveNext(); ) { ...

How are Python's Built In Dictionaries Implemented

The topic title pretty much says it all. Does anyone know how the built in dictionary type for python is implemented? My understanding is that it is some sort of hash table, but I haven't been able to find any sort of definitive answer. ...

Storing and updating lists in Python dictionaries: why does this happen?

Hello, I have a list of data that looks like the following: // timestep,x_position,y_position 0,4,7 0,2,7 0,9,5 0,6,7 1,2,5 1,4,7 1,9,0 1,6,8 ... and I want to make this look like: 0, (4,7), (2,7), (9,5), (6,7) 1, (2,5), (4,7), (9,0), (6.8) My plan was to use a dictionary, where the value of t is the key for the dictionary, and t...

Hashtable to Dictionary<> syncroot . .

Hashtables have a syncroot property but generic dictionaries dont. If i have code that does this: lock (hashtable.Syncroot) { .... } How i do i replicate this if i am removing the hashtable and changing to generic dictionaries. ...

Passing a dictionary to a function in python as keyword parameters

I'd like to call a function in python using a dictionary. Here is some pseudo-code: d = dict(param='test') def f(param): print param f(d) This prints {'param': 'test'} but I'd like it to just print test. I'd like it to work similarly for more parameters: d = dict(p1=1, p2=2) def f2(p1,p2): print p1, p2 f2(d) Is this pos...

Is there a Collection that works like a Dictionary without the values?

I need a collection that works just like a Dictionary/Hastable in a sense that it will only contain 1 instance of a given object at any time. A generic class would be preferable, but I'll take what I can get. Does this collection live somewhere in the framework? ...