dictionary
Get the last element in a dictionary?
What do you mean by Last? Do you mean Last value added? The Dictionary<TKey,TValue> class is an unordered collection. Adding and removing items can change what is considered to be the first and last element. Hence there is no way to get the Last element added. There is an ordered dictionary class available in the … Read more
In Python, How can I get the next and previous key:value of a particular key in a dictionary?
Edit: OP now states that they are using OrderedDicts but the use case still requires this sort of approach. Since dicts are not ordered you cannot directly do this. From your example, you are trying to reference the item like you would use a linked list. A quick solution would be instead to extract the … Read more
Float values as dictionary key
There’s no problem using floats as dict keys. Just round(n, 1) them to normalise them to your keyspace. eg. >>> hash(round(6.84, 1)) 3543446220 >>> hash(round(6.75, 1)) 3543446220
Python: How to turn a dictionary of Dataframes into one big dataframe with column names being the key of the previous dict?
You can try first set_index of all dataframes in comprehension and then use concat with remove last level of multiindex in columns: print d {‘17012016’: Fruit Price 0 Orange 7 1 Apple 8 2 Pear 9, ‘16012016’: Fruit Price 0 Orange 4 1 Apple 5 2 Pear 6, ‘15012016’: Fruit Price 0 Orange 1 1 … Read more
How do I extract all the values of a specific key from a list of dictionaries?
If you just need to iterate over the values once, use the generator expression: generator = ( item[‘value’] for item in test_data ) … for i in generator: do_something(i) Another (esoteric) option might be to use map with itemgetter – it could be slightly faster than the generator expression, or not, depending on circumstances: from … Read more
Python: Create Dictionary from Text/File that’s in Dictionary Format
You can use the eval built-in. For example, this would work if each dictionary entry is on a different line: dicts_from_file = [] with open(‘myfile.txt’,’r’) as inf: for line in inf: dicts_from_file.append(eval(line)) # dicts_from_file now contains the dictionaries created from the text file Alternatively, if the file is just one big dictionary (even on multiple … Read more
Efficiently accessing arbitrarily deep dictionaries
I got a 20% performance boost by tightening up the code a bit but a whopping 400% increase by using a cache for split strings. That only makes a difference if you use the same spec multiple times. Here are sample implementations and a profile script to test. test.py mydict = { ‘first’: { ‘second’: … Read more
Partial match for the key of a std::map
You can’t efficiently search for substring, but you can for prefix: #include <iostream> #include <map> #include <string> #include <algorithm> using namespace std; typedef map<string, string> TStrStrMap; typedef pair<string, string> TStrStrPair; TStrStrMap::const_iterator FindPrefix(const TStrStrMap& map, const string& search_for) { TStrStrMap::const_iterator i = map.lower_bound(search_for); if (i != map.end()) { const string& key = i->first; if (key.compare(0, search_for.size(), … Read more
Dictionary access speed comparison with integer key against string key
CPython’s dict implementation is in fact optimized for string key lookups. There are two different functions, lookdict and lookdict_string (lookdict_unicode in Python 3), which can be used to perform lookups. Python will use the string-optimized version until a search for non-string data, after which the more general function is used. You can look at the … Read more