Python data structure and their C++ equivalent - python

I am looking for python equivalent to these data structure in C++
map<string,set<string>>
sorted by key
the value to be a set
and
map<key,val>
sorted by key
After reading this: https://docs.python.org/2/library/collections.html#collections.OrderedDict
I still didn't find anything suitable.

In python do you have 4 data structures: lists, tuples, sets and dicts.
You can create your own structure combining them:
First:
data = {'my_set':set()}
Second:
data = {'key': 'value'}
And you can sort this data structures using the collections module.
How can I sort a dictionary by key?

There is no perfect mapping between c++ and python; python is dynamically typed.
map<string,set<string>> is equivalent to (using OrderedDict to preserve the order of the keys, otherwise, dict suffice.):
OrderedDict(), where the keys are strings, and the values are sets in which you insert strings
from collections import OrderedDict
a = OrderedDict()
a['my_key'] = set()
a['my_key'].add('my_string')
note. The OrderedDict will not be ordered "by key", but by insertion
order of the keys (thanks to #timgeb in the comments)

Related

Changing order of dictionaries keys based on preferred order

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.

How to facilitate dict record filtering by dict key value?

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.

Why json.keys() get keys in different order [duplicate]

This question already has answers here:
How to convert json string to dictionary and save order in keys? [duplicate]
(1 answer)
Can I get JSON to load into an OrderedDict?
(6 answers)
Closed 9 years ago.
Hi i have the following code in python:
import json
jsonpost = json.loads('{"Player": {"_id": "3","firstName":"kim","surname":"jones"},"newTable": {"_id": "4","SurName": "abcd"}}')
for key in jsonpost.keys():
for innerkey in jsonpost[key]:
print innerkey
my question is why when i print out inner key the order of the keys for jsonpost['Player'] is the following:
_id , surname, firstName
Python dictionaries are implemented internally with a hash table. This means that key order is not preserved. Is this an issue?
keys in a dictionary are never in the same order, if you want them consistently in the same order for your code, you could do
sorted(jsonpost.keys())
Either JavaScript's JSON nor Python's dict have the concept of ordering. You can use collections.OrderedDict in Python to get away with it, but JavaScript does not have such an alternative.
Python dictionaries are inherently unordered, so when you print them out, the keys can be out of order.
If order is important, you can use an OrderedDict:
import collections
jsonpost = collections.OrderedDict(sorted(jsonpost.items()))
Or you can change to for loop to:
for key in sorted(jsonpost.keys()):
for innerkey in jsonpost[key]:
print innerkey
Dictionary ordering is specified as being unspecified, you could sort(whatever.keys()) or you could use and ordered dictionary, i.e. collections.OrderedDict if you are running python 2.7 or later.

How to maintain order of key/value pairs in dictionary of python? [duplicate]

This question already has answers here:
How do you retrieve items from a dictionary in the order that they're inserted?
(11 answers)
Closed 3 years ago.
I am creating a module in which a dictionary is used. This dictionary contains some key/value pairs, whose order must be preserved when using in other modules. But whenever I iterate over dictionary and print the keys, the keys are not in the order in which they key/value pairs are inserted.
So my question is how should I preserve the order of key/value pair in a dictionary?
Any help will be highly appreciated...
Thanx in advance...
We know that regular Python dictionaries iterate over pairs in an arbitrary order, hence they do not preserve the insertion order of pairs.
Python 2.7. introduced a new “OrderDict” class in the “collections” module and it provides the same interface like the general dictionaries but it traverse through keys and values in an ordered manner depending on when a key was first inserted.
Eg:
from collections import OrderedDict
d = OrderDict([('Company-id':1),('Company-Name':'Intellipaat')])
d.items() # displays the output as: [('Company-id':1),('Company-Name':'Intellipaat')]

Will Dict Return Keys and Values in Same Order? [duplicate]

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.

Categories