dictionary

Duplicate keys in .NET dictionaries?

Are there any dictionary classes in the .NET base class library which allow duplicate keys to be used? The only solution I've found is to create, for example, a class like: Dictionary<string, List<object>> But this is quite irritating to actually use. In Java, I believe a MultiMap accomplishes this, but cannot find an analog in .NET...

Why are entries in addition order in a .Net Dictionary?

I just saw this behaviour and I'm a bit surprised by it... If I add 3 or 4 elements to a Dictionary, and then do a "For Each" to get all the keys, they appear in the same order I added them. The reason this surprises me is that a Dictionary is supposed to be a HashTable internally, so I expected things to come out in ANY order (ordered...

Using lock on the key of a Dictionary<string, object>

I have a Dictionary<string, someobject>. EDIT: It was pointed out to me, that my example was bad. My whole intention was not to update the references in a loop but to update different values based on differnt threads need to update/get the data. I changed the loop to a method. I need to update items in my dictionary - one key at a time...

python dictionary update method

I have a list string tag. I am trying to initialize a dictionary with the key as the tag string and values as the array index. for i, ithTag in enumerate(tag): tagDict.update(ithTag=i) The above returns me {'ithTag': 608} 608 is the 608th index My problem is that while the i is being interpreted as a variable, Python is treatin...

Splitting a semicolon-separated string to a dictionary, in Python

I have a string that looks like this: "Name1=Value1;Name2=Value2;Name3=Value3" Is there a built-in class/function in Python that will take that string and construct a dictionary, as though I had done this: dict = { "Name1": "Value1", "Name2": "Value2", "Name3": "Value3" } I have looked through the modules available but ...

Convert dictionary values into array

what is the most efficient way of turning the list of values of a dictionary into an array for example, if i have: Dictionary where key is string and value is Foo i want to get Foo[] I am using VS 2005, C# 2.0 ...

Javascript fake dictionarys

I've declared Javascript arrays in such a way that I could then access them by a key, but it was a long time ago, and I've forgotten how I did it. Basically, I have two fields I want to store, a unique key, and its value. I know there is a way to do it.. something like: var jsArray = new {key: 'test test', value: 'value value'}, ...

Map two lists into a dictionary in Python

Imagine that you have: keys = ('name', 'age', 'food') values = ('Monty', 42, 'spam') What is the simplest way to produce the following dictionary ? dict = {'name' : 'Monty', 'age' : 42, 'food' : 'spam'} This code works, but I'm not really proud of it : dict = {} junk = map(lambda k, v: dict.update({k: v}), keys, values) ...

Dictionary API or Library

Does anyone know of a good dictionary API or ruby library to lookup the definitions of words? I'm thinking it should work something like: I call get_definition(word) It returns the definition for that word (ideally in some way to easily format the definition for display. Thanks ...

Python Disk-Based Dictionary

Hello, I was running some dynamic programming code (trying to brute-force disprove the Collatz conjecture =P) and I was using a dict to store the lengths of the chains I had already computed. Obviously, it ran out of memory at some point. Is there any easy way to use some variant of a dict which will page parts of itself out to disk whe...

Given a list of variable names in Python, how do I a create a dictionary with the variable names as keys (to the variables' values)?

I have a list of variable names, like this: ['foo', 'bar', 'baz'] (I originally asked how I convert a list of variables. See Greg Hewgill's answer below.) How do I convert this to a dictionary where the keys are the variable names (as strings) and the values are the values of the variables? {'foo': foo, 'bar': bar, 'baz': baz} No...

Is there a concise catalog of variable naming-conventions?

There are many different styles of variable names that I've come across over the years. The current wikipedia entry on naming conventions is fairly light... I'd love to see a concise catalog of variable naming-conventions, identifying it by a name/description, and some examples. If a convention is particularly favored by a certain...

Formatting dict.items() for wxPython

I have a text box in wxPython that takes the output of dictionary.items() and displays it to the user as items are added to the dictionary. However, the raw data is very ugly, looking like [(u'BC',45) (u'CHM',25) (u'CPM',30)] I know dictionary.items() is a list of tuples, but I can't seem to figure out how to make a nice format that ...

Map two lists into one single list of dictionaries

Imagine I have these python lists: keys = ['name', 'age'] values = ['Monty', 42, 'Matt', 28, 'Frank', 33] Is there a direct or at least a simple way to produce the following list of dictionaries ? [ {'name': 'Monty', 'age': 42}, {'name': 'Matt', 'age': 28}, {'name': 'Frank', 'age': 33} ] ...

C# dictionaries ValueOrNull / ValueorDefault

Currently I'm using var x = dict.ContainsKey(key) ? dict[key] : defaultValue I'd like some way to have dictionary[key] return null for nonexistant keys, so I could write something like var x = dict[key] ?? defaultValue; this also winds up being part of linq queries etc. so I'd prefer one-line solutions. ...

Concurrency handling using the filesystem VS an RDMBS (MySQL)

I'm building an English web dictionary where users can type in words and get definitions. I thought about this for a while and since the data is 100% static and I was only to retrieve one word at a time I was better off using the filesystem (ext3) as the database system instead of opting to use MySQL to store definitions. I figured there...

When I iterate through a Dictionary (.NET generic data structure) will it be in the same order that I added them?

I have a dictionary that I normally access with a key, so I need fast random access reads. However for one function I need to process every item in the dictionary where the order is important. It seems to be working OK in tests. Is it OK to depend on the order of items in a dictionary? ...

Where can I obtain dictionary files for use in checking spelling?

I thought this was asked before, but 15 minutes of searching on Google and the site search didn't turn anything up...so: Where can I obtain free (as in beer and/or as in speech) dictionary files? I'm mainly interested in English, but if you know of any dictionary files, please point them out. Note: This question doesn't have a right/wr...

Hashtable/dictionary/map lookup with regular expressions

I'm trying to figure out if there's a reasonably efficient way to perform a lookup in a dictionary (or a hash, or a map, or whatever your favorite language calls it) where the keys are regular expressions and strings are looked up against the set of keys. For example (in Python syntax): >>> regex_dict = { re.compile(r'foo.') : 12, re.c...

C#: How can Dictionary<K,V> implement ICollection<KeyValuePair<K,V>> without having Add(KeyValuePair<K,V>)?

Looking at System.Collections.Generic.Dictionary<TKey, TValue>, it clearly implements ICollection<KeyValuePair<TKey, TValue>>, but doesn't have the required "void Add(KeyValuePair<TKey, TValue> item)" function. This can also be seen when trying to initialize a Dictionary like this: private const Dictionary<string, int> PropertyIDs = ne...