ordereddictionary

Python: Failed in retrieving the highest amount from a repeated data with different amount in a certain year

☆樱花仙子☆ 提交于 2020-01-06 05:48:11
问题 The csv file that I have contain several repeated supplier_name but with different amt for year 2015-2017. Here goes my codes. df = pd.read_csv('government-procurement-via-gebiz.csv', parse_dates=['award_date'], infer_datetime_format=True, usecols=['supplier_name', 'award_date', 'awarded_amt'],) df = df[(df['supplier_name'] != 'na') & (df['award_date'].dt.year == 2015)].reset_index(drop=True) d1 = df.set_index('supplier_name').to_dict()['awarded_amt'] top5D1 = dict(sorted(d1.iteritems(), key

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

风格不统一 提交于 2020-01-02 04:05:36
问题 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

Why can't I create a default, ordered dict by inheriting OrderedDict and defaultdict?

拜拜、爱过 提交于 2020-01-01 08:27:01
问题 My first attempt to combine the features of two dictionaries in the collections module was to create a class that inherits them: from collections import OrderedDict, defaultdict class DefaultOrderedDict(defaultdict, OrderedDict): def __init__(self, default_factory=None, *a, **kw): super().__init__(default_factory, *a, **kw) However, I cannot assign an item to this dictionary: d = DefaultOrderedDict(lambda: 0) d['a'] = 1 Traceback (most recent call last): File "<stdin>", line 1, in <module>

Write Python OrderedDict to CSV

与世无争的帅哥 提交于 2019-12-30 22:33:13
问题 I have an ordered dictionary that, as one would expect, contains a number of key-value pairs. I need to parse the contents of the ordered dictionary into CSV format, with the keys in the top row and their respective values in the second row. I've got the first row down, but I'm at a loss as to how to write the second row. The values in the OrderedDict are what you'd expect (what it looks like printed out in the terminal): ([(a, 1), (b, 2), (c, 3)]) . Here's what I have: import csv with open(

Generate a pandas dataframe from ordereddict?

谁说我不能喝 提交于 2019-12-28 13:45:24
问题 I am trying to create a pandas dataframe from an ordereddict to preserve the order of the values. But for some reason after creating the dataframe the fields are messed up again. Here's the list of ordereddicts: [OrderedDict([ ('key_a', 'value_a'), ('key_b', 'value_b'), ]), OrderedDict([ ('key_a', 'value_c'), ('key_b', 'value_d'), ]) ] Now how should I create a pandas DataFrame from these? What I am looking for is something like that (the important thing is the key_a and key_b etc column name

Convert OrderedDict to normal dict preserving order?

拈花ヽ惹草 提交于 2019-12-25 07:27:33
问题 How do I convert an OrderedDict to a normal dictionary while preserving the same order? The reason I am asking this is because is when I fetch my data from an API, I get a JSON string, in which I use json.loads(str) to return a dictionary. This dictionary that is returned from json.loads(...) is just out of order and is randomly ordered. Also, I've read that OrderedDict is slow to work with so I want to use regular dict in same order as original JSON string. Slightly off-topic: Is there

Get key count from OrderedDict where key is a tuple

我与影子孤独终老i 提交于 2019-12-24 12:15:13
问题 I've a dictionary such as this: my_dict=collections.OrderedDict([((123, 1), 'qwe'), ((232, 1), 'asd'), ((234, 2), 'zxc'), ((6745, 2), 'aaa'), ((456, 3), 'bbb')]) The combination of the tuple is always unique and I would like to maintain the order of insertion, and hence OrderedDict. I've a well over ~10K items in the dict. How can I efficiently maintain a counter that gives the count of the second element in the tuple? Basically, I need to know the count whenever I would like to add/delete an

Does Python-Requests support OrderedDicts, or is something else going wrong here?

蹲街弑〆低调 提交于 2019-12-24 01:18:11
问题 I'm trying to POST a request to an Amazon S3 endpoint using Python's Requests library. The request is of the multipart/form-data variety, because it includes the POSTing of an actual file. One requirement specified by the API I'm working against is that the file parameter must be posted last . Since Requests uses dictionaries to POST multipart/form-data, and since dictionaries don't follow a dictated order, I've converted it into an OrderedDict called payload . It looks something like this

In an OrderedDict how to sort by a particular attribute? [closed]

烈酒焚心 提交于 2019-12-24 00:39:02
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I am trying to sort the following OrderedDict by doing the following--> >>> from collections import OrderedDict >>> d = OrderedDict([(4995L, [{'isbn_13': u'9788131805923', 'book_quantity': 49L, 'seller_book_id': 4995L, 'book_id': 4995L, 'title': u'Industrial Automation and Robotics', 'selling_price': 292.0, 'id'

How to test order-conscious equality of hashes

大憨熊 提交于 2019-12-23 12:04:56
问题 Ruby 1.9.2 introduced order into hashes. How can I test two hashes for equality considering the order? Given: h1 = {"a"=>1, "b"=>2, "c"=>3} h2 = {"a"=>1, "c"=>3, "b"=>2} I want a comparison operator that returns false for h1 and h2 . Neither of the followings work: h1 == h2 # => true h1.eql? h2 # => true 回答1: Probably the easiest is to compare the corresponding arrays. h1.to_a == h2.to_a 回答2: You could compare the output of their keys methods: h1 = {one: 1, two: 2, three: 3} # => {:one=>1,