dictionary

is locking necessary for Dictionary lookup?

lock(dictionaryX) { dictionaryX.TryGetValue(key, out value); } is locking necessary while doing lookups to a Dictionary ? THe program is multithreaded, and while adding key/value to dict. dict is being locked. ...

Get Dictionary key by using the dictionary value

How to get the dictionary key by using the dictionary value? when getting the value using the key its like this: Dictionary<int, string> dic = new Dictionary<int, string>(); dic.Add(1, "a"); Console.WriteLine(dic[1]); Console.ReadLine(); How to do the opposite? ...

non-destructive version of pop() for a dictionary

Is there any idiom for getting an arbitrary key, value pair from a dictionary without removing them? (P3K) EDIT: Sorry for the confusing wording. I used the word arbitrary in the sense that I don't care about what I'm getting. It's different from random, where I do care about what I'm getting (i.e., I need probabilities of each item ...

Dictionary Manipulation Using LINQ in C#

I'm having a Dictionary like Dictionary<String, List<String>> MyDict = new Dictionary<string, List<string>> { {"One",new List<String>{"A","B","C"}},{"Two",new List<String>{"A","C","D"}} }; I need to get a List<String> from this dictionary, The List should contain Distinct items from the values of the above ...

C#:Conversion of Dictionary<String,String> to Dictionary<String, Dictionary<String,String>>

The First Dictionary is like Dictionary<String, String> ParentDict = new Dictionary<String, String>(); ParentDict.Add("A_1", "1"); ParentDict.Add("A_2", "2"); ParentDict.Add("B_1", "3"); ParentDict.Add("B_2", "4"); ParentDict.Add("C_1", "5"); i need to convert this into a new Dictionary...

Delphi Generics > Dictionary with default value

Hi all, I would like to have a dictionary that returns a default value when the search key is not found. Reading from the documentation: Generics.Collections.Tdictionary […] This class provides a mapping […] and initial content. 1 - How? Is there a way to do it ala Python: {1: ‘one’; 2:’two’} ? Generics.Collections.TDictionary.TryGet...

Is Dictionary<TKey, TValue> adding empty elements in the backing storage?

I am looking into the code behind Dictionary<TKey, TValue>. What is interesting is in the private Insert method, there is a bucket that appears to be holding empty slots in a pre-sized array. Inside the Insert method, the code checks to see if the bucket has any elements left and will resize if necessary. The number of elements added is ...

Appending at run-time to a dictionary

I have a function named OpenAccount() which takes in the details from the User and appends it to my database dictionary. I have a database file(module) which is imported in my function file. I have a function called AppendRecord(key,**dictvalues) which appends values to my database file. However I am not able to call the function and ...

Can't read appended data using pickle.load() method.

I have written two scripts Write.py and Read.py. Write.py opens friends.txt in append mode and takes input for name, email ,phone no and then dumps the dictionary into the file using pickle.dump() method and every thing works fine in this script. Read.py opens friends.txt in read mode and then loads the contents into dictionary using p...

The order of elements in Dictionary

Hi, My question is about enumerating Dictionary elements // Dictionary definition private Dictionary<string, string> _Dictionary = new Dictionary<string, string>(); // add values using add _Dictionary.Add("orange", "1"); _Dictionary.Add("apple", "4"); _Dictionary.Add("cucumber", "6"); // add values using [] _Dictionary["banana"] = ...

How do I take integer keys in shelve ?

I want to store an integer key in shelve. But when I try to store integer key in shelve it give me an error Traceback (most recent call last): File "./write.py", line 12, in data[id] = {"Id": id, "Name": name} File "/usr/lib/python2.5/shelve.py", line 124, in __setitem__ self.dict[key] = f.getvalue() File "/usr/lib/pytho...

A python class that acts like dict

I want to write a custom class that behaves like dict - so, I am inheriting from dict. My question though is that do I need to create a private dict member in my init() method?. I dont see the point of this, since I already have the dict behaviour if I simply inherit from dict. Can anyone point out why most of the inheritance snippets ...

Accessing a dictionary from methods outside main in C#

Hi, I am creating a dictionary and then populating it with entries in main(), then calling a method which uses said dictionary. Short of including the dictionary in the arguments being passed to this method, how can I access it without getting the error 'An object reference is required for the non-static field, method or property 'XXX.Y...

C# Merging 2 dictionaries

I'm developing an app in C# targeting .NET 3.5. In it, I have 2 similar dictionaries that contain validation criteria for a specific set of elements in my app. Both dictionaries have identical signatures. The first dictionary has the default settings and the 2nd dictionary contains some user defined settings. var default_settings = ...

Accessing English dictionary on iOS

I want users to be able to look up word definitions from inside my iPad App. Is there a way to access the English dictionary in iOS, the way iBooks does, or do I need to go to an outside API? ...

How can we modify data that is in a shelve?

I have opened a shelve using the following code: #!/usr/bin/python import shelve #Module:Shelve is imported to achieve persistence Accounts = 0 Victor = {'Name':'Victor Hughes','Email':'[email protected]','Deposit':65000,'Accno':'SA456178','Acctype':'Savings'} Beverly = {'Name':'Beverly Dsilva','Email':'[email protected]...

An item with the same key has already been added c# error

foreach (Benefit bene in claim.SubClaimFolderCollection[0].BenefitsFolder.BenefitCollection) { Dictionary<string, object> searchBeneficiary = new Dictionary<string,object>(); searchBeneficiary.Add("rli_beneficiariesid", ((Guid)claimant.GetValue("rli_subclaimfolderid", true))); } It gives me this error An it...

splitting a dictionary in python into keys and values.

How can I take a dictionary and split it into two lists, one of keys, one of values. For example take: {'name': 'Han Solo', 'firstname': 'Han', 'lastname': 'Solo', 'age': 37, 'score': 100, 'yrclass': 10} and split it into: ['name', 'firstname', 'lastname', 'age', 'score', 'yrclass'] # and ['Han Solo', 'Han', 'Solo', 36, 100, 10] An...

Memory efficient int-int dict in Python

Hi, I need a memory efficient int-int dict in Python that would support the following operations in O(log n) time: d[k] = v # replace if present v = d[k] # None or a negative number if not present I need to hold ~250M pairs, so it really has to be tight. Do you happen to know a suitable implementation (Python 2.7)? EDIT Removed i...

EXC_BAD_ACCESS - iPhone

Hi My iPhone app is crashing and getting EXC_BAD_ACCESS error when using this code: NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1 NSString *documentsDirectory = [paths objectAtIndex:0]; //2 NSString *path = [documentsDirectory stringByAppend...