ordereddictionary

OrderedDict in python 3 - how to get the keys in order?

怎甘沉沦 提交于 2019-12-10 13:35:58
问题 In python 2 when using an OrderedDict I was able to get the keys in the order they were inserted by simply using the keys method that returned a list. In python 3 however: rows = OrderedDict() rows[0]=[1,2,3] rows[1]=[1,2,3] image = [rows[k] for k in rows.keys()[:2]] I get: Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: 'odict_keys' object is not subscriptable I certainly can do list(rows)[:2] as advised for instance here - but is this guaranteed to get me

How to know the position of items in a Python ordered dictionary

拥有回忆 提交于 2019-12-09 14:00:26
问题 Can we know the position of items in Python's ordered dictionary? For example: If I have dictionary: // Ordered_dict is OrderedDictionary Ordered_dict = {"fruit": "banana", "drinks": "water", "animal": "cat"} Now how do I know in which position cat belongs to? Is it possible to get an answer like: position (Ordered_dict["animal"]) = 2 ? or in some other way? 回答1: You may get a list of keys with the keys property: In [20]: d=OrderedDict((("fruit", "banana"), ("drinks", 'water'), ("animal",

string to OrderedDict conversion in python

谁说胖子不能爱 提交于 2019-12-07 15:00:25
问题 i have created a python Ordered Dictionary by importing collections and stored it in a file named 'filename.txt'. the file content looks like OrderedDict([(7, 0), (6, 1), (5, 2), (4, 3)]) i need to make use of this OrderedDict from another program. i do it as myfile = open('filename.txt','r') mydict = myfile.read() i need to get 'mydict' as of Type <class 'collections.OrderedDict'> but here, it comes out to be of type 'str'. is there any way in python to convert a string type to OrderedDict

Python OrderedDict ordered by date

时间秒杀一切 提交于 2019-12-07 13:06:38
问题 I am trying to use an OrderedDict (Raymond Hettingers version for pre2.7 Python) where my keys are dates. However it does not order them correctly, I imagine it may be ordering based on the ID. Does anyone have any suggestions of how this could be done? 回答1: OrderedDict, according to its docstring, is a kind of dict that remembers insertion order. Thus, you need to manually insert the key/value pairs in the correct order. # assuming unordered_dict is a dict that contains your data ordered

Python OrderDict sputtering as compared to dict()

空扰寡人 提交于 2019-12-07 06:27:13
问题 This one has me entirely baffled. asset_hist = [] for key_host, val_hist_list in am_output.asset_history.items(): for index, hist_item in enumerate(val_hist_list): #row = collections.OrderedDict([("computer_name", key_host), ("id", index), ("hist_item", hist_item)]) row = {"computer_name": key_host, "id": index, "hist_item": hist_item} asset_hist.append(row) This code works perfectly with the collections line commented out. However, when I comment out the row = dict line and remove the

string to OrderedDict conversion in python

偶尔善良 提交于 2019-12-05 23:48:37
i have created a python Ordered Dictionary by importing collections and stored it in a file named 'filename.txt'. the file content looks like OrderedDict([(7, 0), (6, 1), (5, 2), (4, 3)]) i need to make use of this OrderedDict from another program. i do it as myfile = open('filename.txt','r') mydict = myfile.read() i need to get 'mydict' as of Type <class 'collections.OrderedDict'> but here, it comes out to be of type 'str'. is there any way in python to convert a string type to OrderedDict type? using python 2.7 You could store and load it with pickle import cPickle as pickle # store: with

Python OrderedDict ordered by date

时光毁灭记忆、已成空白 提交于 2019-12-05 19:25:15
I am trying to use an OrderedDict (Raymond Hettingers version for pre2.7 Python) where my keys are dates. However it does not order them correctly, I imagine it may be ordering based on the ID. Does anyone have any suggestions of how this could be done? OrderedDict, according to its docstring, is a kind of dict that remembers insertion order. Thus, you need to manually insert the key/value pairs in the correct order. # assuming unordered_dict is a dict that contains your data ordered_dict = OrderedDict() for key, value in sorted(unordered_dict.iteritems(), key=lambda t: t[0]): ordered_dict[key

How to turn pandas dataframe row into ordereddict fast

限于喜欢 提交于 2019-12-05 10:46:52
问题 Looking for a fast way to get a row in a pandas dataframe into a ordered dict with out using list. List are fine but with large data sets will take to long. I am using fiona GIS reader and the rows are ordereddicts with the schema giving the data type. I use pandas to join data. I many cases the rows will have different types so I was thinking turning into a numpy array with type string might do the trick. 回答1: Unfortunately you can't just do an apply (since it fits it back to a DataFrame):

Ansible - with_dict: dictionary - How to use variables defined in each dictionary which depends upon others

。_饼干妹妹 提交于 2019-12-05 10:29:39
Environment is: Ansible 1.9.2, CentOS 6.5 I have created a role to download JAVA (.tar.gz) artifact files for 3 different JAVA versions from Artifactory. I'm trying to use Ansible's with_dict feature (instead of using with_items). Created the following files: $ cat roles/java/defaults/main.yml --- java_versions: java7_60: version: 1.7.60 group_path: com/oracle/jdk classifier: linux-x64 ext: tar.gz dist_file: "jdk-{{ version }}-{{ classifier }}-{{ ext }}" # dist_file: "jdk-{{item.value.version }}-{{ item.value.classifier }}-{{ item.value.ext }}" dist_url: "{{ artifactory_url }}/{{ group_path }}

What is the best data structure in .NET for look-up by string key or numeric index?

試著忘記壹切 提交于 2019-12-04 22:56:13
问题 I'm looking for the most ideal data structure (for performance and ease of use) from which values can be retrieved by string key or index. Dictionary doesn't work because you can't really retrieve by index. Any ideas? 回答1: You want the OrderedDictionary class. You will need to include the System.Collections.Specialized namespace: OrderedDictionary od = new OrderedDictionary(); od.Add("abc", 1); od.Add("def", 2); od.Add("ghi", 3); od.Add("jkl", 4); // Can access via index or key value: Console