dictionary

Django: Converting an entire Model into a single dictionary

Is there a good way in Django to convert an entire model to a dictionary? I mean, like this: class DictModel(models.Model): key = models.CharField(20) value = models.CharField(200) DictModel.objects.all().to_dict() ... with the result being a dictionary with the key/value pairs made up of records in the Model? Has anyone els...

Why isn't there an XML-serializable dictionary in .NET?

I need an XML-serializable dictionary. Actually, I now have two quite different programs that need one. I was rather surprised to see that .NET doesn't have one. I asked the question elsewhere and got sarcastic responses. I don't understand why it's a stupid question. Can someone enlighten me, given how dependent various .NET featur...

most efficient data structure for a read-only list of strings (about 100,000) with fast prefix search

I'm writing an application that needs to read a list of strings from a file, save them in a data structure, and then look up those strings by prefixes. The list of strings is simply a list of words in a given language. For example, if the search function gets "stup" as a parameter, it should return ["stupid", "stupidity", "stupor"...]. I...

C#: Is it possible to use expressions or functions as keys in a dictionary?

Would it work to use Expression<Func<T>> or Func<T> as keys in a dictionary? For example to cache the result of heavy calculations. For example, changing my very basic cache from a different question of mine a bit: public static class Cache<T> { // Alternatively using Expression<Func<T>> instead private static Dictionary<Func<T...

In C# .NET, is there a reason for no copy constructor for StringDictionary?

I apologize if this is a dumb question, but hear me out: Dictionary<string, string> genericDict = new Dictionary<string, string>; genericDict.Add("blah", "bloop"); // Use the copy constructor to create a copy of this dictionary return new Dictionary<string, string>(genericDict); In the above code sample, I can create a copy of a gener...

C#: Is a SortedDictionary sorted when you enumerate over it?

A SorteDictionary is according to MSDN sorted on the key. Does that mean that you can be sure that it will be sorted when you enumerate it in a foreach? Or does it just mean that the SortedDictionary works that way internally to have better performance in various cases? ...

.NET - JSON Data - Deserialization - Lists & Dictionaries

I need to pull back from a database JSON documents that are not based on a standard object. Is there a way using .NET to "deserialize" these documents into Lists & Dictionaries of primitive objects (string, int, bool, etc...) Any library that can do this in both directions? ...

AS3: Extending The Dictionary Class - Accessing Stored Data

So I want to extend the dictionary class. Everything works so far except that in some of my methods that need to reference the dictionary's content I make a call like: this[ key ] It doesn't like that. It just tells me that there's no property 'key'. Is there a way to way to access the data within this class? Also, I'm using an inte...

Should I check whether particular key is present in Dictionary before accessing it?

Should I check whether particular key is present in Dictionary if I am sure it will be added in dictionary by the time I reach the code to access it? There are two ways I can access the value in dictionary checking ContainsKey method. If it returns true then I access using indexer [key] of dictionary object. or TryGetValue which ...

Class Dictionary in java (data structure)

How to use Class Dictionary in java ? ...

Optimizing a Dictionary for comparison when the key is based upon several identifying attributes.

I have the following code: if (!this._masterAccountDefinitions.ContainsKey( outletName.ToLower(CultureInfo.CurrentCulture) + "/" + officeName.ToLower(CultureInfo.CurrentCulture)) ) { //Add new item to list } Essentially, the dictionary is keyed by the combination of an Outlet Name and Office Name. This code will be ...

What's the best way to set all values in a C# Dictionary<string,bool>?

What's the best way to set all values in a C# Dictionary? Here is what I am doing now, but I'm sure there is a better/cleaner way to do this: Dictionary<string,bool> dict = GetDictionary(); var keys = dict.Keys.ToList(); for (int i = 0; i < keys.Count; i++) { dict[keys[i]] = false; } I have tried some other ways with foreach, but...

How do I get the sum of the Counts of nested Lists in a Dictionary without using foreach?

I want to get the total number of items in the Lists in the following Dictionary: Dictionary<int, List<string>> dd = new Dictionary<int, List<string>>() { {1, new List<string> {"cem"}}, {2, new List<string> {"cem", "canan"}}, {3, new List<string> {"canan", "cenk", "cem"}} }; // This only returns an enumerated array. var i =...

How to I add an item to a Dictionary<string, Dictionary<string, object>>?

I want users of my LayoutManager class to be able to write this: LayoutManager layoutManager = new LayoutManager(); layoutManager.AddMainContentView("customers", "intro", new View { Title = "Customers Intro", Content = "This is customers intro." }); But what syntax do I need to fill this dictionary in a dictionary in AddMainConte...

What is the best data structure for tree-like data of fixed depth in C#?

What is the optimal (easy to maintain, reasonably fast, robust) implementation of tree-like data structure with three levels? I would like to use Dictionary or SortedDictionary, because all values (nodes have unique key). The first level is supposed to have about 300 items, every of these zero to tens (hardly more than 100, usually less...

How do I get the nth element from a Dictionary?

cipher = new Dictionary<char,int>; cipher.Add( 'a', 324 ); cipher.Add( 'b', 553 ); cipher.Add( 'c', 915 ); How to get the 2nd element? For example, I'd like something like: KeyValuePair pair = cipher[1] where pair contains ( 'b', 553 ) thanks! Based on thecoop's suggestion using a List, things are workking: List<KeyValuePair<ch...

explicit and implicit c#

Hi I'm new to C# and learning new words. I find it difficult to understand what's the meaning of these two words when it comes to programming c#. I looked in the dictionary for the meaning and here's what I got: Implicit "Something that is implicit is expressed in an indirect way." "If a quality or element is implicit in som...

Is there a more elegant way of adding an item to a Dictionary<> safely?

I need to add key/object pairs to a dictionary, but I of course need to first check if the key already exists otherwise I get a "key already exists in dictionary" error. The code below solves this but is clunky. What is a more elegant way of doing this without making a string helper method like this? using System; using System.Collecti...

JSON Serialization with class inheriting from Dictionary<T,V>

I have a class that's currently inheriting from Dictionary and then adds a few first class member properties to it. Roughly: public class Foo : Dictionary<string, string> { public string Bar { get; set; } public string Baz { get; set; } } Upon serializing an instance of this object to JSON however, it appears that the serialize...

one liner for conditionally replacing dictionary values

Is there a better way to express this using list comprehension? Or any other way of expressing this in one line? I want to replace each value in the original dictionary with a corresponding value in the col dictionary, or leave it unchanged if its not in the col dictionary. col = {'1':3.5, '6':4.7} original = {'1':3, '2':1, '3':5, '4':...