The input is a dictionary, for example:
{'first_name':'Jane', 'occupation': 'astronaut', 'age':27, 'last_name':'Doe'}
The keys need to be rearranged to be in a specific order, given in a list, for example:
preferred_order = ['first_name', 'last_name', 'age', 'location']
The dictionary might not have all the keys in the preferred_order list, and might have keys that don't appear on the list.
In this specific case, the result of the sorting should be:
{'first_name':'Jane', 'last_name':'Doe', 'age':27, 'occupation': 'astronaut'}
Key location didn't get added to the dictionary, and the keys not in preferred_order are at the end.
Suggested algorithm:
Create a new, initially empty, dictionary;
Iterate through preferred_order: for every key in preferred_order, add key: value to the new dictionary if it exists in the old dictionary, and remove it from the old dictionary;
for every remaining key: value pair in the old dictionary, add it to the new dictionary.
For step 3, you can use dict.update or |=.
Further reading:
documentation on dict.update and |=;
more about |=;
How do I sort a dictionary by value?;
You can search for "sort dictionary by key" on stackoverflow, but be aware that most answers are outdated and recommend using collections.OrderedDict instead of dict, which is no longer necessary since Python 3.7.
Related
I want to interface with rocksdb in my python application and store arbitrary dicts in it. I gather that for that I can use something like pickle to for serialisation. But I need to be able to filter the records based on values of their keys. What's the proper approach here?
so let's say you have a list of keys named dict_keys and you have a dict named big_dict and you want to filter out only the values from dict_keys. You can write a dict comprehension that iterates through the list grabbing the items from the dict if they exist like this:
new_dict = {key: big_dict.get(key) for key in dict_keys}
RocksDB is a key-value store, and both key and value are binary strings.
If you want to filter by given keys, just use the Get interface to search the DB.
If you want to filter by given key patterns, you have to use the Iterator interface to iterating the whole DB, and filter the records with keys that match the pattern.
If you want to filter by values or value patterns, you still need to iterating the whole DB. For each key-value pair, deserialize the value, and check if it equals to the give value or matches the give pattern.
For case 1 and case 2, you don't need to deserialize all values, but only values that equal to the give key or match the pattern. However, for case 3, you have to deserialize all values.
Both case 2 and case 3 are inefficient, since they need to iterate the whole key space.
You can configure RocksDB's key to be ordered, and RocksDB have a good support for prefix indexing. So you can efficiently do range query and prefix query by key. Check the documentation for details.
In order to efficiently do value filter/search, you have to create a value index with RocksDB.
I have a dictionary with some share related data:
share_data = {'2016-06-13':{'open': 2190, 'close':2200}, '2015-09-10':{'open': 2870, 'close':2450} # and so on, circa 1,500 entries
is there a way of iterating over the dictionary in order, so the oldest date is retrieved first, then the one soon after etc?
thanks!
Sure, the default lexicographical order of your date strings will map to chronological order. So it is very easy:
for key in sorted(share_data.keys()):
#do something
This post has some nice examples of custom sorting on dictionaries.
I want to create an ordered dictionary with a List as the value type.
I tried to call this method:
ordered = collections.OrderedDict(list)
but I get the error:
TypeError: 'type' object is not iterable
Is there any other data structure I can use for an ordered dictionary?
Later in the program I need to get the first key/value pair that was inserted; that's why I need the list ordered. After that point order does not matter.
Python is dynamically typed. You don't need to specify the value type in advance, you just insert objects of that type when needed.
For example:
ordered = collections.OrderedDict()
ordered[123] = [1,2,3]
You can get the first inserted key/value with next(ordered.iteritems()) (Python 2) or next(ordered.items()) (Python 3).
I've read the question already about using a list, dictionary or a tuple but it doesn't answer my specified question
if I wanted an array say with the values of
array(1,2,3,4, "name" => "someone", "age" => array())
should I use a list or a dictionary? also, is it possible to have a multidimensional list or dictionary?
Order does matter here, also, I want to be able to access them using the key, for example
array["name"] would return "someone"
Seeing your use-case only a dictionary would help, because what you are expecting is to pass a key not an index to array. And only a dict takes key against which it returns a value. Unlike list which take in an index.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python dictionary: are keys() and values() always the same order?
If i have a dictonary in python, will .keys and .values return the corresponding elements in the same order?
E.g.
foo = {'foobar' : 1, 'foobar2' : 4, 'kittty' : 34743}
For the keys it returns:
>>> foo.keys()
['foobar2', 'foobar', 'kittty']
Now will foo.values() return the elements always in the same order as their corresponding keys?
It's hard to improve on the Python documentation:
Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond. This allows the creation of (value, key) pairs using zip(): pairs = zip(d.values(), d.keys()). The same relationship holds for the iterkeys() and itervalues() methods: pairs = zip(d.itervalues(), d.iterkeys()) provides the same value for pairs. Another way to create the same list is pairs = [(v, k) for (k, v) in d.iteritems()]
So, in short, "yes" with the caveat that you must not modify the dictionary in between your call to keys() and your call to values().
Yes, they will
Just see the doc at Python doc :
Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond.
Best thing to do is still to use dict.items()
From the Python 2.6 documentation:
Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond. This allows the creation of (value, key) pairs using zip(): pairs = zip(d.values(), d.keys()). The same relationship holds for the iterkeys() and itervalues() methods: pairs = zip(d.itervalues(), d.iterkeys()) provides the same value for pairs. Another way to create the same list is pairs = [(v, k) for (k, v) in d.iteritems()].
I'm over 99% certain the same will hold true for Python 3.0.