ordereddictionary

Why are the values of an OrderedDict not equal?

拟墨画扇 提交于 2019-11-28 06:09:37
With Python 3: >>> from collections import OrderedDict >>> d1 = OrderedDict([('foo', 'bar')]) >>> d2 = OrderedDict([('foo', 'bar')]) I wanted to check for equality: >>> d1 == d2 True >>> d1.keys() == d2.keys() True But: >>> d1.values() == d2.values() False Do you know why values are not equal? I've tested this with Python 3.4 and 3.5. Following this question, I posted on the Python-Ideas mailing list to have additional details: https://mail.python.org/pipermail/python-ideas/2015-December/037472.html In Python 3, dict.keys() and dict.values() return special iterable classes - respectively a

Any way to properly pretty-print ordered dictionaries?

北慕城南 提交于 2019-11-28 03:44:26
I like the pprint module in Python. I use it a lot for testing and debugging. I frequently use the width option to make sure the output fits nicely within my terminal window. It has worked fine until they added the new ordered dictionary type in Python 2.7 (another cool feature I really like). If I try to pretty-print an ordered dictionary, it doesn't show nicely. Instead of having each key-value pair on its own line, the whole thing shows up on one long line, which wraps many times and is hard to read. Does anyone here have a way to make it print nicely, like the old unordered dictionaries? I

Dictionary sorting by key length

瘦欲@ 提交于 2019-11-28 02:02:09
Anyone have any idea how to sort this dictionary by key length? { 'http://ccc.com/viewvc/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.14'}, {'type': 'operating-systems', 'app': 'Ubuntu', 'ver': None}], 'http://bbb.com/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.22'}, {'type': 'programming-languages', 'app': 'PHP', 'ver': '5.3.10'}, {'type': 'cms', 'app': 'Drupal', 'ver': None}, {'type': 'operating-systems', 'app': 'Ubuntu', 'ver': None}, {'type': 'javascript-frameworks', 'app': 'jQuery', 'ver': None}, {'type': 'captchas', 'app': 'Mollom', 'ver': None}] } Expected

OrderedDict vs Dict in python

纵然是瞬间 提交于 2019-11-27 23:23:08
问题 In Tim Peter's answer to "Are there any reasons not to use an ordered dictionary", he says OrderedDict is a subclass of dict. It's not a lot slower, but at least doubles the memory over using a plain dict. Now, while going through a particular question, I tried some sample checks using ipython and both of them contradict the earlier reasoning: both dict and OrderedDict are of same size operating on an OrderedDict takes easily around 7-8 times more time than operating on a dict (Hence a lot

OrderedDictionary and Dictionary

时间秒杀一切 提交于 2019-11-27 18:27:59
I was looking for a way to have my Dictionary enumerate its KeyValuePair in the same order that they were added. Now, Dictionary's documentation clearly states that: For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair<TKey, TValue> structure representing a value and its key. The order in which the items are returned is undefined. I found out that what I needed was an OrderedDictionary , but being the sceptic that I am, I decided to try it myself: OrderedDictionary od = new OrderedDictionary(); Dictionary<String, String> d = new Dictionary<String, String>();

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

为君一笑 提交于 2019-11-27 11:37:20
What's the correct way to initialize an ordered dictionary (OD) so that it retains the order of initial data? from collections import OrderedDict # Obviously wrong because regular dict loses order d = OrderedDict({'b':2, 'a':1}) # An OD is represented by a list of tuples, so would this work? d = OrderedDict([('b',2), ('a', 1)]) # What about using a list comprehension, will 'd' preserve the order of 'l' l = ['b', 'a', 'c', 'aa'] d = OrderedDict([(i,i) for i in l]) Question: Will an OrderedDict preserve the order of a list of tuples, or tuple of tuples or tuple of lists or list of lists etc.

How to convert an OrderedDict into a regular dict in python3

孤人 提交于 2019-11-27 11:16:40
I am struggling with the following problem: I want to convert an OrderedDict like this: OrderedDict([('method', 'constant'), ('data', '1.225')]) into a regular dict like this: {'method': 'constant', 'data':1.225} because I have to store it as string in a database. After the conversion the order is not important anymore, so I can spare the ordered feature anyway. Thanks for any hint or solutions, Ben >>> from collections import OrderedDict >>> OrderedDict([('method', 'constant'), ('data', '1.225')]) OrderedDict([('method', 'constant'), ('data', '1.225')]) >>> dict(OrderedDict([('method',

Accessing items in an collections.OrderedDict by index

拜拜、爱过 提交于 2019-11-27 06:10:52
Lets say I have the following code: import collections d = collections.OrderedDict() d['foo'] = 'python' d['bar'] = 'spam' Is there a way I can access the items in a numbered manner, like: d(0) #foo's Output d(1) #bar's Output If its an OrderedDict() you can easily access the elements by indexing by getting the tuples of (key,value) pairs as follows >>> import collections >>> d = collections.OrderedDict() >>> d['foo'] = 'python' >>> d['bar'] = 'spam' >>> d.items() [('foo', 'python'), ('bar', 'spam')] >>> d.items()[0] ('foo', 'python') >>> d.items()[1] ('bar', 'spam') Note for Python 3.X dict

How to convert an OrderedDict into a regular dict in python3

为君一笑 提交于 2019-11-27 05:12:01
问题 I am struggling with the following problem: I want to convert an OrderedDict like this: OrderedDict([('method', 'constant'), ('data', '1.225')]) into a regular dict like this: {'method': 'constant', 'data':1.225} because I have to store it as string in a database. After the conversion the order is not important anymore, so I can spare the ordered feature anyway. Thanks for any hint or solutions, Ben 回答1: >>> from collections import OrderedDict >>> OrderedDict([('method', 'constant'), ('data',

Using a list as a data source for DataGridView

送分小仙女□ 提交于 2019-11-27 04:22:06
I've extracted the setting names and their respective values out of a configuration file into an ordered dictionary. The dictionary contains keys and values which are of the ICollection class. I want to bind that data and display it in a DataGridView. I've tried copying the strings to arrays and displaying those arrays, but when I ran the program the columns were blank and it did not seem to be binded at all. I've also attempted to set the DataGridView source directly to one the ordered dictionary collections (keys or values), but that also did not result in anything I wanted; the columns were