dictionary

Limiting the size of a python dictionary

I'd like to work with a dict in python, but limit the number of key/value pairs to X. In other words, if the dict is currently storing X key/value pairs and I perform an insertion, I would like one of the existing pairs to be dropped. It would be nice if it was the least recently inserted/accesses key but that's not completely necessary....

A problem with compare item value

Hi, I have defined my class: public class Host { public string Name; } then a strongly-typed dictionary: Dictionary<string, Host> HostsTable; then I try to compare a value: if (HostsTable.Values.Where(s => s.Name == "myhostname") != null) { doSomething } and the problem is, nothing is found, even I'm sure the item is on t...

KeyNotFound Exception in Dictionary(of T)

I'm about ready to bang my head against the wall I have a class called Map which has a dictionary called tiles. class Map { public Dictionary<Location, Tile> tiles = new Dictionary<Location, Tile>(); public Size mapSize; public Map(Size size) { this.mapSize = size; } //etc... I fill this dictionary tem...

Distinct Value Array for View Controller Index Using Core Data

Hi, I'm trying to create an index representing the first letter value of each record in a Core Data store to be used in a table view controller. I'm using a snippet of the code from Apple's documentation. I would simply like to produce an array or dictionary of distinct values as the result. My store already has the character defined wi...

get dictionary key by value

How do I get a Dictionary key by value in C#? Dictionary<string, string> types = new Dictionary<string, string>() { {"1", "one"}, {"2", "two"}, {"3", "three"} }; I want something like this: getByValueKey(string value); getByValueKey("one") must be return "1". What is the best way do this? Maybe ...

Composite key in Dictionary; override GetHashCode(), Equals etc or use structs?

I have quite a few dictionaries where the key is a composite of several different values (mostly strings and integers). Do I implement these keys as classes (and override GetHashCode(), Equals() etc) or do I use struct instead? ReSharper makes it easy to do the overriding, but the code looks horrible. Are there any performance implicat...

Please help with a dictionary comprehension in LINQ, C#.

Python's equivalent of what I want is: >>> #C#: Dictionary<int, string> tempDict = ... >>> tempDict = {i : str(i) for i in range(200000)} >>> tempDict[5] '5' >>> The example is a bit simplified, but I can modify it myself; do not want to bother you with details of proprietary classes. Got it: var y = (from x in Enumerable.Range(0, ...

Do you know of a C dictionary that supports COW transactions?

I'm looking for a key -> value dictionary library written in C that supports a theoretically unlimited number of cheap transactions. I'd like to have one dictionary in memory, with hundreds of threads starting transactions, possibly modifying the dictionary, ending (completing) the transaction or potentially aborting the transaction. On...

Quick question about a reference type key in a generic dictionary in .Net

I have a mutable class that I'm using as a key to a generic dictionary. Two keys should be equal only if their references are equal. From what I've read, in this case, I don't need to override Equals, GetHashCode , or implement IEqualityComparer. Is this correct? ...

Dictionary Lookup (O(1)) vs Linq where

What is faster and should I sacrifice the Linq standard to achieve speed (assuming Dictionary lookup is truly faster)? So let me elaborate: I have the following: List<Product> products = GetProductList(); I have a need to search for a product based on some attribute, for example, the serial number. I could first create a dictionary...

Looking for a syntactic shortcut for accessing dictionaries

I have an abstract base class that holds a Dictionary. I'd like inherited classes to be able to access the dictionary fields using a convenient syntax. Currently I have lots of code like this: string temp; int val; if (this.Fields.TryGetValue("Key", out temp)) { if (int.TryParse(temp, out val)) { // do something with val... ...

C# Get Keys from a Dictionary<string, Stream>

Suppose I have a Dictionary like so: Dictionary<string, Stream> How can I get a list (or IEnumerable or whatever) of JUST the Keys from this dictionary? Is this possible? I could enumerate the dictionary, and extract the keys one by one, but I was hoping to avoid this. In my instance, the Dictionary contains a list of filenames (fil...

Approach to mapping dictionary database tables to models in MVC

Hi, lacking a fellow programmer to talk over the right approach for my problem, I decided to ask you. What is your preferred approach of mapping dictionary tables to a model in MVC paradigm, regardless of the MVC framework / environment you are using? My problem is I have a couple of database tables that only serve as dictionaries and ...

Get the dictionary values for every key in a list

Let's say I have a list: a = ['apple', 'carrot'] and a dictionary: d ={'apple': [2,4], 'carrot': [44,33], 'orange': [345,667]} How can I use the list a as a key to lookup in the dictionary d? I want the result to be written to a comma-separated textfile like this apple, carrot 2, 44 4, 33 Corrected the a-list ...

how to copy a dictionary in python 3.1 and edit ONLY the copy

can someone please explain this to me??? this doesn't make any sense to me.... I copy a dictionary into another and edit the second and both are changed???? ActivePython 3.1.0.1 (ActiveState Software Inc.) based on Python 3.1 (r31:73572, Jun 28 2009, 19:55:39) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or...

cpython: when PyDict_GetItem is called and when dict_subscript?

I am reading cpython code for python 3k and I have noticed, that __missing__ is called only when dict_subscript is called, but not when PyDict_GetItem is used. What is the difference between those two methods and when each is called? If I pass an PyObject that is a subclass of dict and has __missing__ method, how can I force using it, si...

Redirect print in Python: val = print(arg) to output mixed iterable to file

So lets say I have an incredibly nested iterable of lists/dictionaries. I would like to print them to a file as easily as possible. Why can't I just redirect print to a file? val = print(arg) gets a SyntaxError. Is there a way to access stdinput? And why does print take forever with massive strings? Bad programming on my side for ...

Which implementation of OrderedDict should be used in python2.6?

As some of you may know in python2.7/3.2 we'll get OrderedDict with PEP372 however one of the reason the PEP existed was because everyone did their own implementation and they were all sightly incompatible. So which one of the 8 current implementations link text is backwards compatible with the 2.7 odict from python 2.7 in a way we can...

How to map IDictionary<string, object> in Fluent NHibernate?

I am looking to persist user preferences into a collection of name value pairs, where the value may be an int, bool, or string. There are a few ways to skin this cat, but the most convenient method I can think of is something like this: public class User { public virtual IDictionary<string, object> Preferences { get; set; } } wit...

Converting JSON into Python dict

I've been searching around trying to find an answer to this question, and I can't seem to track it down. Maybe it's too late in the evening to figure the answer out, so I turn to the excellent readers here. I have the following bit of JSON data that I am pulling out of a CouchDB record: "{\"description\":\"fdsafsa\",\"order\":\"1\",\"...