dictionary

Concatenate tuples in empty dict

errors = {} #errorexample errors['id'] += ('error1',) errors['id'] += ('error2',) #works but ugly errors['id'] = ('error1',) errors['id'] += ('error2',) If 'error1' is not present it will fail. Do I really have to extend dict? ...

How to assign/add item(s) to a Dictionary using the key index?

We wanted to assign/add an item to a Dictionary using the key index, like this: Dictionary<string, object> dict = new Dictionary<string, object>(); dict["key"] = dict["key"] ?? "object"; But it results to: "The given key was not present in the dictionary." Is there any way we could assign values to this dictionary the same as the way...

How can I check if there exist any reverse element in list of dict without looping on it

My list is like l1 = [ {k1:v1} , {k2:v2}, {v1:k1} ] Is there any better way to check if any dictionary in the list is having reverse pair? ...

Serialising and Deserialising V.Large Dictionary in C#

We have a v.large Dictionary<long,uint> (several million entries) as part of a high performance C# application. When the application closes we serialise the dictionary to disk using BinaryFormatter and MemoryStream.ToArray(). The serialisation returns in about 30 seconds and produces a file about 200MB in size. When we then try to deseri...

Is it totally fine to use a mutable object as a key in a Dictionary?

Say I have some special class, WrappedDataTable, and I want to associate each WrappedDataTable with exactly one DataTable. Furthermore, I want there to be no more than one WrappedDataTable in existence for any given DataTable. A colleague suggested I could cache my WrappedDataTable and use a factory method to access one, like this: pub...

In a Python dict of dicts, how do you emulate Perl's auto-vivification behavior?

Both Google and the online docs are not delivering much insight on my query, so I thought I would ask the community here. In Perl, you can easily setup a hash-of-a-hash-of-a-hash and test the final key like so: my $hash = {}; $hash{"element1"}{"sub1"}{"subsub1"} = "value1"; if (exists($hash{"element1"}{"sub1"}{"subsub1"})) { print "...

Cast generic dictionary in C#.

Is there a way to cast a dictionary i a one-liner and without all the overhead in C# .net 3? var result = new Dictionary<string, AccessoryVariant>(); foreach (BaseVariant variant in mVariants.Values) { result.Add(variant.Id, (AccessoryVariant)variant); } return result; I would like to do something like this: return (Dictionary<st...

Is Dictionary<string,object> the best way to pass an unknown collection of variables to a constructor?

I have a number of "section items" (Lesson, Info) which inherit from the common type SectionItem. The various types of SectionItems share some but not all properties. I have found the best way to pass parameters to each kind of object is to pack them all in a Dictionary<string, object> and then let the base class SectionItem unpack the...

Generating a good hash code (GetHashCode) for a BitArray

I need to generate a fast hash code in GetHashCode for a BitArray. I have a Dictionary where the keys are BitArrays, and all the BitArrays are of the same length. Does anyone know of a fast way to generate a good hash from a variable number of bits, as in this scenario? UPDATE: The approach I originally took was to access the internal...

Python Dictionary

I am in simple doubt... I created the following dictionary: >>> alpha={'a': 10, 'b': 5, 'c': 11} But, when I want to see the dictionary keys and values I got: >>> alpha {'a': 10, 'c': 11, 'b': 5} See that the "b" and "c" has swapped their position. How can I make the position be the same of the moment that the dictionary was create...

Dictionary<string, List<int>> to WPF GridView

I would like to bind a Dictionary<string, List<int>> to a GridView in a ListView and I'm not having much luck. I've looked at this question: wpf-binding-dictionarystring-liststring-to-listview-listbox-how, and this question: dictionary-binding-to-listview But I'm not sure how to take their code and make it work for me. Here's what I'v...

Fluent Nhibernate - Map many to many dictionary with enum key

I'm trying to do a mapping for the following classes: public class A { public virtual int Id { get; private set; } public virtual IDictionary<MyEnum, B> MyBValues { get; set; } } public class B { public virtual int Id { get; private set; } } public enum MyEnum { Value1, Value2, Value3 } I'd like the resulting tables to look...

Using Python's @property decorator on dicts

I'm trying to use Python's @property decorator on a dict in a class. The idea is that I want a certain value (call it 'message') to be cleared after it is accessed. But I also want another value (call it 'last_message') to contain the last set message, and keep it until another message is set. In my mind, this code would work: >>> class...

Do dictionaries in Python offer the best way to formulate switch-like statements?

I'm hoping there's no performance or other disadvantage in attempting to avoid long chains of conditional if/elif statements this way: errstr = {404: "404 Not Found", 405: "405 Method Not Allowed"} if code in errstr: print errstr[code]; ...

What's an elegant way to create a dictionary from another dictionary's keys and an array of values?

How can I create another dictionary using the keys of another dictionary, and an array of values? I thought about doing this: zipped = zip(theExistingDict.keys(), arrayOfValues) myNewDict = dict(zipped) However, this doesn't quite work, each value from arrayOfValues are paired with an arbitrary key in the resulting dictionary. I don'...

Android Dictionary Imaplementation

Hi friends, I want to implement dictionary in my simple Android Application. I want to check spell of every word writing in my text view. How can i implement this one. please suggest me. Thanks in Advance. ...

Best way to store a list of definitions for fastest lookup

I have some sort of dictionary file that looks like this: UTM University of Tennessee at Martin UMD University of Maryland It is a 3 letters acronym followed by the definition, separated by newlines. In total the file has 9282 definitions. My questions are: 1) What would be the best way to store this definitions? Should I place th...

populating and and accessing data from a value object

I have have a problem loading and accessing data from a value object in my new project.. I load an xml file via a service, which contains title and locations of asset files, I need to be able to access the location of an asset file by specifying the title and retrieiving it from a value object.. I'm using the Robotlegs framework, here's ...

Nhibernate - Mapping many to many dictionary

I have the following classes: public class A { public virtual int Id { get; private set; } public virtual IDictionary<MyEnum, B> MyBValues { get; set; } } public class B { public virtual int Id { get; private set; } } public enum MyEnum { Value1, Value2, Value3 } And would like the following tables: A ------------...

Routines for suggesting alternatives

Hi, I have been tasked with coming up with a routine that will suggest alternative domain names to register if the customers original requested domain name is already registered. The first step I think would be to split the requested domain back in to bits so that I could work out alternatives to try. eg. mybigredtruck.com would be b...