How to sort OrderedDict of OrderedDict?

You’ll have to create a new one since OrderedDict is sorted by insertion order. In your case the code would look like this: foo = OrderedDict(sorted(foo.items(), key=lambda x: x[1][‘depth’])) See http://docs.python.org/dev/library/collections.html#ordereddict-examples-and-recipes for more examples. Note for Python 2 you will need to use .iteritems() instead of .items().

OrderedDictionary and Dictionary

You are doing it wrong. You need not only to insert values sequentially into dictionary, but also remove some elements and see how the order has changed after this. The next code demonstrates this: OrderedDictionary od = new OrderedDictionary(); Dictionary<String, String> d = new Dictionary<String, String>(); Random r = new Random(); for (int i = … Read more

Last element in OrderedDict

Using next(reversed(od)) is a perfect way of accessing the most-recently added element. The class OrderedDict uses a doubly linked list for the dictionary items and implements __reversed__(), so this implementation gives you O(1) access to the desired element. Whether it is worthwhile to subclass OrderedDict() for this simple operation may be questioned, but there’s nothing … Read more

Any way to properly pretty-print OrderedDict?

Ever since Python 3.7, Python guarantees that keys in a dictionary will retain their insertion order. (They still don’t behave exactly the same as OrderedDict objects, though, as two dicts a and b can be considered equal a == b even if the order of the keys is different, whereas OrderedDict does check this upon … Read more

Right way to initialize an OrderedDict using its constructor such that it retains order of initial data?

The OrderedDict will preserve any order that it has access to. The only way to pass ordered data to it to initialize is to pass a list (or, more generally, an iterable) of key-value pairs, as in your last two examples. As the documentation you linked to says, the OrderedDict does not have access to … Read more

How to convert an OrderedDict into a regular dict in python3

>>> from collections import OrderedDict >>> OrderedDict([(‘method’, ‘constant’), (‘data’, ‘1.225’)]) OrderedDict([(‘method’, ‘constant’), (‘data’, ‘1.225’)]) >>> dict(OrderedDict([(‘method’, ‘constant’), (‘data’, ‘1.225’)])) {‘data’: ‘1.225’, ‘method’: ‘constant’} >>> However, to store it in a database it’d be much better to convert it to a format such as JSON or Pickle. With Pickle you even preserve the order!

Converting dict to OrderedDict

You are creating a dictionary first, then passing that dictionary to an OrderedDict. For Python versions < 3.6 (*), by the time you do that, the ordering is no longer going to be correct. dict is inherently not ordered. Pass in a sequence of tuples instead: ship = [(“NAME”, “Albatross”), (“HP”, 50), (“BLASTERS”, 13), (“THRUSTERS”, … Read more

No generic implementation of OrderedDictionary?

Implementing a generic OrderedDictionary isn’t terribly difficult, but it’s unnecessarily time consuming and frankly this class is a huge oversight on Microsoft’s part. There are multiple ways of implementing this, but I chose to use a KeyedCollection for my internal storage. I also chose to implement various methods for sorting the way that List<T> does … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)