Posts

Showing posts with the label dictionary

Convert lines of string to a dictionary

3 I have an initial code like this: record = "Jane,Doe,25/02/2002; James,Poe,19/03/1998; Max,Soe,16/12/2001 ..." I need to make it into a dictionary and its output should be something like this: {'First name': 'Jane', 'Last name': 'Doe', 'Birthday': '25/02/2002'} {'First name': 'James', 'Last name': 'Poe', 'Birthday': '19/03/1998'} ... Each line should have an incrementing key starting from 1. I currently have no idea to approach this issue as I am still a student with no prior experience. I have seen people use this for strings containing key-value pairs but my string does not contain those: mydict = dict((k.strip(), v.strip()) ...

map in clojure giving unexpected result

1 Using map in clojure, checking whether a string contains uppercase character. (map #(= (clojure.string/upper-case %) %) "Hello") The expected result. (true false false false false) unfortunately, the result is unexpected. (false false false false false) I did an experiment when I replace "H" in the first "%", the result is still unexpected. (map #(= (clojure.string/upper-case "H") %) "Hello") (false false false false false) When I replace "H" in the second "%", the result is changed, it is an expected result. (map #(= (clojure.string/upper-case %) "H) "Hello") (true false false false false) What's wrong with that? Please feel free to comment. ...

How to sum elements in list of dictionaries if two key values are the same

I have the following list of dictionaries: dictionary =[{'Flow': 100, 'Location': 'USA', 'Name': 'A1'}, {'Flow': 90, 'Location': 'Europe', 'Name': 'B1'}, {'Flow': 20, 'Location': 'USA', 'Name': 'A1'}, {'Flow': 70, 'Location': 'Europe', 'Name': 'B1'}] I want to create a new list of dictionaries, with summed Flow values of all dictionaries where Location and Name are the same. My desired output would be: new_dictionary =[{'Flow': 120, 'Location': 'USA', 'Name': 'A1'}, {'Flow': 160, 'Location': 'Europe', 'Name': 'B1'},] How can I achieve this? This is possible, but non-trivial to implement in python. Might I suggest using pandas? This is simple with a groupby, sum, and to_dict. import pandas as pd (pd...

Converting list of lists into a dictionary of dictionaries in Python

I am trying to convert a list of lists data structure to a dictionary of dictionaries. The list is defined as follows: l = [ ['PP','Ear-rings', 'Holesovice', 2000], ['PP','Skirts', 'Holesovice', 1000], ['PP','Dresses', 'E-shop', 1500], ['BM','Butterfly', 'Holesovice', 1600] ] My aim is to have the dictionary structure as follows: #{'PP' : {'Holesovice' : {'Ear-rings' : 2000, 'Skirts' : 1000}, # 'E-shop' : {'Dresses' : 1500}}, # 'BM' : {'Holesovice' : {'Butterfly' : 1600}} #} This bit of code does not return desired output: labels_d = {} items_d = {} shops_d = {} for index, row in enumerate(l): items_d[row[1]] = row[3] shops_d[row[2]] = items_d labels_d[row[0]] = shops_d print(labels_d) I found some posts that deal with converting lists to dictionaries here and here but I did not make it work the way...