ordereddictionary

Override the {…} notation so i get an OrderedDict() instead of a dict()?

旧城冷巷雨未停 提交于 2019-11-26 08:49:22
问题 I want to use a .py file like a config file. So using the {...} notation I can create a dictionary using strings as keys but the definition order is lost in a standard python dictionary. My question: is it possible to override the {...} notation so that I get an OrderedDict() instead of a dict() ? I was hoping that simply overriding dict constructor with OrderedDict ( dict = OrderedDict ) would work, but it doesn\'t. Eg: dict = OrderedDict dictname = { \'B key\': \'value1\', \'A key\': \

How to sort OrderedDict of OrderedDict?

房东的猫 提交于 2019-11-26 08:36:46
问题 I\'m trying to sort OrderedDict in OrderedDict by \'depth\' key. Is there any solution to sort that Dictionary ? OrderedDict([ (2, OrderedDict([ (\'depth\', 0), (\'height\', 51), (\'width\', 51), (\'id\', 100) ])), (1, OrderedDict([ (\'depth\', 2), (\'height\', 51), (\'width\', 51), (\'id\', 55) ])), (0, OrderedDict([ (\'depth\', 1), (\'height\', 51), (\'width\', 51), (\'id\', 48) ])), ]) Sorted dict should look like this: OrderedDict([ (2, OrderedDict([ (\'depth\', 0), (\'height\', 51), (\

Creating an Ordered Counter

戏子无情 提交于 2019-11-26 07:43:49
问题 I\'ve been reading into how super() works. I came across this recipe that demonstrates how to create an Ordered Counter: from collections import Counter, OrderedDict class OrderedCounter(Counter, OrderedDict): \'Counter that remembers the order elements are first seen\' def __repr__(self): return \'%s(%r)\' % (self.__class__.__name__, OrderedDict(self)) def __reduce__(self): return self.__class__, (OrderedDict(self),) For example: oc = OrderedCounter(\'adddddbracadabra\') print(oc)

No generic implementation of OrderedDictionary?

北城以北 提交于 2019-11-26 02:33:27
问题 There doesn\'t appear to be a generic implementation of OrderedDictionary (which is in the System.Collections.Specialized namespace) in .NET 3.5. Is there one that I\'m missing? I\'ve found implementations out there to provide the functionality, but wondered if/why there isn\'t a generic implementation out-of-the-box and if anyone knows whether it\'s something in .NET 4.0? 回答1: You're right. There's no generic equivalent of OrderedDictionary in the framework itself. (That's still the case for

Converting dict to OrderedDict

徘徊边缘 提交于 2019-11-26 00:59:22
问题 I am having some trouble using the collections.OrderedDict class. I am using Python 2.7 on Raspbian, the Debian distro for Raspberry Pi. I am trying to print two dictionaries in order for comparison (side-by-side) for a text-adventure. The order is essential to compare accurately. No matter what I try the dictionaries print in their usual unordered way. Here\'s what I get when I do it on my RPi: import collections ship = {\"NAME\": \"Albatross\", \"HP\":50, \"BLASTERS\":13, \"THRUSTERS\":18,

Rename a dictionary key

不问归期 提交于 2019-11-26 00:32:03
问题 Is there a way to rename a dictionary key, without reassigning its value to a new name and removing the old name key; and without iterating through dict key/value? In case of OrderedDict, do the same, while keeping that key\'s position. 回答1: For a regular dict, you can use: mydict[new_key] = mydict.pop(old_key) For an OrderedDict, I think you must build an entirely new one using a comprehension. >>> OrderedDict(zip('123', 'abc')) OrderedDict([('1', 'a'), ('2', 'b'), ('3', 'c')]) >>> oldkey,

Can I get JSON to load into an OrderedDict?

只谈情不闲聊 提交于 2019-11-25 22:58:13
问题 Ok so I can use an OrderedDict in json.dump . That is, an OrderedDict can be used as an input to JSON. But can it be used as an output? If so how? In my case I\'d like to load into an OrderedDict so I can keep the order of the keys in the file. If not, is there some kind of workaround? 回答1: Yes, you can. By specifying the object_pairs_hook argument to JSONDecoder. In fact, this is the exact example given in the documentation. >>> json.JSONDecoder(object_pairs_hook=collections.OrderedDict)

Converting dict to OrderedDict

99封情书 提交于 2019-11-25 21:29:41
I am having some trouble using the collections.OrderedDict class. I am using Python 2.7 on Raspbian, the Debian distro for Raspberry Pi. I am trying to print two dictionaries in order for comparison (side-by-side) for a text-adventure. The order is essential to compare accurately. No matter what I try the dictionaries print in their usual unordered way. Here's what I get when I do it on my RPi: import collections ship = {"NAME": "Albatross", "HP":50, "BLASTERS":13, "THRUSTERS":18, "PRICE":250} ship = collections.OrderedDict(ship) print ship # OrderedDict([('PRICE', 250), ('HP', 50), ('NAME',

Rename a dictionary key

痞子三分冷 提交于 2019-11-25 18:45:41
Is there a way to rename a dictionary key, without reassigning its value to a new name and removing the old name key; and without iterating through dict key/value? In case of OrderedDict, do the same, while keeping that key's position. For a regular dict, you can use: mydict[new_key] = mydict.pop(old_key) For an OrderedDict, I think you must build an entirely new one using a comprehension. >>> OrderedDict(zip('123', 'abc')) OrderedDict([('1', 'a'), ('2', 'b'), ('3', 'c')]) >>> oldkey, newkey = '2', 'potato' >>> OrderedDict((newkey if k == oldkey else k, v) for k, v in _.viewitems())