ordereddictionary

Using an ordered dict as object dictionary in python

和自甴很熟 提交于 2019-12-23 07:58:10
问题 I don't know why this doesn't work: I'm using the odict class from PEP 372, but I want to use it as a __dict__ member, i.e.: class Bag(object): def __init__(self): self.__dict__ = odict() But for some reason I'm getting weird results. This works: >>> b = Bag() >>> b.apple = 1 >>> b.apple 1 >>> b.banana = 2 >>> b.banana 2 But trying to access the actual dictionary doesn't work: >>> b.__dict__.items() [] >>> b.__dict__ odict.odict([]) And it gets weirder: >>> b.__dict__['tomato'] = 3 >>> b

Python select ith element in OrderedDict

ぃ、小莉子 提交于 2019-12-22 02:05:23
问题 I have a snippet of code which orders a dictionary alphabetically. Is there a way to select the ith key in the ordered dictionary and return its corresponding value? i.e. import collections initial = dict(a=1, b=2, c=2, d=1, e=3) ordered_dict = collections.OrderedDict(sorted(initial.items(), key=lambda t: t[0])) print(ordered_dict) OrderedDict([('a', 1), ('b', 2), ('c', 2), ('d', 1), ('e', 3)]) I want to have some function along the vein of... select = int(input("Input dictionary index"))

Sorting a nested OrderedDict by key, recursively

巧了我就是萌 提交于 2019-12-21 03:33:23
问题 Say orig is an OrderedDict which contains normal string:string key value pairs, but sometimes the value could be another, nested OrderedDict . I want to sort orig by key, alphabetically (ascending), and do it recursively . Rules: Assume key strings are unpredictable Assume nesting can take place infinitely, e.g. level 1-50 all have both strings, OrderedDicts, etc as values. Need an assist with the sorted algorithm: import string from random import choice orig = OrderedDict(( ('a', choice

Last element in OrderedDict

血红的双手。 提交于 2019-12-20 09:50:39
问题 I have od of type OrderedDict . I want to access its most recently added (key, value) pair. od.popitem(last = True) would do it, but would also remove the pair from od which I don't want. What's a good way to do that? Can /should I do this: class MyOrderedDict(OrderedDict): def last(self): return next(reversed(self)) 回答1: 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

Find a given key's value in a nested ordered dict python

社会主义新天地 提交于 2019-12-20 04:23:47
问题 I am trying to find the value of a given key from a nested OrderedDict. Key points: I don't know how deep this dict will be nested The name of the key I am looking for is constant, it will be somewhere in the dict I would like to return the value of the key called "powerpoint_color" in this example... mydict= OrderedDict([('KYS_Q1AA_YouthSportsTrustSportParents_P', OrderedDict([('KYS_Q1AA', OrderedDict([('chart_layout', '3'), ('client_name', 'Sport Parents (Regrouped)'), ('sort_order',

Iterate over python dictionary to retrieve only required rows

大憨熊 提交于 2019-12-20 04:10:02
问题 I am getting the data in HTML table format from external source - from xml.etree import ElementTree as ET s = """<table> <tr><th>Release</th><th>REFDB</th><th>URL</th></tr> <tr><td>3.7.3</td><td>12345</td><td>http://google.com</td></tr> <tr><td>3.7.4</td><td>456789</td><td>http://foo.com</td></tr> </table> """ For converting html table to dictionary table = ET.XML(s) rows = iter(table) headers = [col.text for col in next(rows)] for row in rows: values = [col.text for col in row] out = dict

Why does the **kwargs mapping compare equal with a differently ordered OrderedDict?

我的未来我决定 提交于 2019-12-19 15:46:31
问题 According to PEP 468: Starting in version 3.6 Python will preserve the order of keyword arguments as passed to a function. To accomplish this the collected kwargs will now be an ordered mapping . Note that this does not necessarily mean OrderedDict . In that case, why does this ordered mapping fail to respect equality comparison with Python's canonical ordered mapping type, the collections.OrderedDict : >>> from collections import OrderedDict >>> data = OrderedDict(zip('xy', 'xy')) >>> def

Implement an ordered dictionary in Robot Framework

六月ゝ 毕业季﹏ 提交于 2019-12-19 10:12:58
问题 I want to have an ordered dictionary in Robot Framework. Is there any libraries which I can use for this purpose? How can I do that? 回答1: I am not aware of any library that has a keyword to create an ordered dict, but creating an ordered dict is simple enough. As of Python 2.7, there is an ordered dictionary available from Python. Robot Framework also defines one that is available in Python/Jython versions prior to 2.7. The Robot Framework OrderedDict has more features than the Python one.

Why does the OrderedDict keys view compare order-insensitive?

你。 提交于 2019-12-18 13:04:23
问题 Why does the OrderedDict keys view compare order-insensitive? >>> from collections import OrderedDict >>> xy = OrderedDict([('x', None), ('y', None)]) >>> yx = OrderedDict([('y', None), ('x', None)]) >>> xy == yx False >>> xy.keys() == yx.keys() True The OrderedDict keys view should arguably behave like an OrderedSet, but instead it behaves the same as dict.keys (i.e. like a usual set ). Same "issue" in python2: >>> xy.viewkeys() == yx.viewkeys() True They are different types, ( odict_keys is

OrderedDict comprehensions

心不动则不痛 提交于 2019-12-17 18:32:16
问题 Can I extend syntax in python for dict comprehensions for other dicts, like the OrderedDict in collections module or my own types which inherit from dict ? Just rebinding the dict name obviously doesn't work, the {key: value} comprehension syntax still gives you a plain old dict for comprehensions and literals. >>> from collections import OrderedDict >>> olddict, dict = dict, OrderedDict >>> {i: i*i for i in range(3)}.__class__ <type 'dict'> So, if it's possible how would I go about doing