dictionary

python: sorting an ordered dictionary

♀尐吖头ヾ 提交于 2021-01-27 13:57:09
问题 I create a dictionary: d[1] = {'a':1, 'b':2} d[2] = {'a':5, 'b':3} d[3] = {'a':3, 'b':2} I then try to sort by a field: d = collections.OrderedDict(sorted(d.items(), key=itemgetter(1))) so that I can output: for key in d: print key, d[key] This sorts on 'a', but I can't figure out how to sort on 'b'. How do I sort on 'b'? EDIT: I'm not sure how this is unclear. I'd like to sort so that the output is ordered by the values in field 'b'. 回答1: Modify your key function so that it explicitly

How to import dictionary to csv

丶灬走出姿态 提交于 2021-01-27 13:26:26
问题 I have to export data from dictionary to csv. Dictionary contains lists. I tried to do export like this with open("info.csv", 'w',newline='')as csvfile: header = ['Club', 'Stadium'] writer = csv.DictWriter(csvfile, fieldnames=header) writer.writeheader() writer.writerow(info) But the result is Club Stadium ['Arsenal, ['Emirates', 'AFC', etc.] 'Villia park',etc.] And i wanted this Club Stadium Arsenal Emirates AFC Villia park How can i do it? 回答1: You can accomplish what you want doing it like

Union of values of two dictionaries merged by key

旧街凉风 提交于 2021-01-27 08:12:24
问题 I have two dictionaries: d1 = {'a':('x','y'),'b':('k','l')} d2 = {'a':('m','n'),'c':('p','r')} How do I merge these two dictionaries to get such result: d3 = {'a':('x','y','m','n'),'b':('k','l'),'c':('p','r')} This works when values of the dictionary are simple type like int or str: d3 = dict([(i,a[i]+b[i]) for i in set(a.keys()+b.keys())]) But I have no idea how deal with lists as values... Notice: my question is different to How to merge two Python dictionaries in a single expression? cause

Convert Python dict of Arrays into a dataframe

心不动则不痛 提交于 2021-01-27 07:42:26
问题 I have a dictionary of arrays like the following: d = {'a': [1,2], 'b': [3,4], 'c': [5,6]} I want to create a pandas dataframe like this: 0 1 2 0 a 1 2 1 b 3 4 2 c 5 6 I wrote the following code: pd.DataFrame(list(d.items())) which returns: 0 1 0 a [1,2] 1 b [3,4] 2 c [5,6] Do you know how can I achieve my goal?! Thank you in advance. 回答1: Pandas allows you to do this in a straightforward fashion: pd.DataFrame.from_dict(d,orient = 'index') >> 0 1 a 1 2 b 3 4 c 5 6 pd.DataFrame.from_dict(d

Convert Python dict of Arrays into a dataframe

我是研究僧i 提交于 2021-01-27 07:36:12
问题 I have a dictionary of arrays like the following: d = {'a': [1,2], 'b': [3,4], 'c': [5,6]} I want to create a pandas dataframe like this: 0 1 2 0 a 1 2 1 b 3 4 2 c 5 6 I wrote the following code: pd.DataFrame(list(d.items())) which returns: 0 1 0 a [1,2] 1 b [3,4] 2 c [5,6] Do you know how can I achieve my goal?! Thank you in advance. 回答1: Pandas allows you to do this in a straightforward fashion: pd.DataFrame.from_dict(d,orient = 'index') >> 0 1 a 1 2 b 3 4 c 5 6 pd.DataFrame.from_dict(d

Convert Python dict of Arrays into a dataframe

可紊 提交于 2021-01-27 07:36:01
问题 I have a dictionary of arrays like the following: d = {'a': [1,2], 'b': [3,4], 'c': [5,6]} I want to create a pandas dataframe like this: 0 1 2 0 a 1 2 1 b 3 4 2 c 5 6 I wrote the following code: pd.DataFrame(list(d.items())) which returns: 0 1 0 a [1,2] 1 b [3,4] 2 c [5,6] Do you know how can I achieve my goal?! Thank you in advance. 回答1: Pandas allows you to do this in a straightforward fashion: pd.DataFrame.from_dict(d,orient = 'index') >> 0 1 a 1 2 b 3 4 c 5 6 pd.DataFrame.from_dict(d

Finding the key corresponding to an item in a dictionary

ⅰ亾dé卋堺 提交于 2021-01-27 07:09:49
问题 Is there any way to find the key that corresponds to a given item in a VBA dictionary? http://msdn.microsoft.com/en-us/library/aa164502%28v=office.10%29.aspx MSDN suggests that the "Key" property can be used, but when I try using it I get an error ("Compile error: invalid use of property"). I've found in the past that the "Exists" method given here doesn't work for me either, so I assume that they were the commands in a previous version of Office and are now outdated. However I haven't been

Redux Spread Operator vs Map

元气小坏坏 提交于 2021-01-27 06:48:51
问题 I have a State of objects in an Array (in my Redux Reducer). const initialState = { items: [ { id: 1, dish: "General Chicken", price: 12.1, quantity: 0 }, { id: 2, dish: "Chicken & Broccoli", price: 10.76, quantity: 0 }, { id: 3, dish: "Mandaran Combination", price: 15.25, quantity: 0 }, { id: 4, dish: "Szechuan Chicken", price: 9.5, quantity: 0 } ], addedItems: [], total: 0 }; I have an action to add 1 to the quantity of an object, such as {id:1, dish: Generals Chicken, price: 10.76,

Sort and group a list of dictionaries

。_饼干妹妹 提交于 2021-01-27 06:41:56
问题 How can I sort and group this list of dictionaries into a nested dictionary which I want to return via an API as JSON. Source Data (list of permissions): [{ 'can_create': True, 'can_read': True, 'module_name': 'ModuleOne', 'module_id': 1, 'role_id': 1, 'end_point_id': 1, 'can_update': True, 'end_point_name': 'entity', 'can_delete': True, }, { 'can_create': True, 'can_read': True, 'module_name': 'ModuleTwo', 'module_id': 2, 'role_id': 1, 'end_point_id': 4, 'can_update': True, 'end_point_name':

Python - How to Create Dictionary from CSV data file using column headings

孤人 提交于 2021-01-27 06:28:38
问题 I am trying to create a function that accepts the name of a .csv data file and a list of strings representing column headings in that file and return a dict object with each key being a column heading and the corresponding value being a numpy array of the values in that column of the data file. My code right now: def columndata(filename, columns): d = dict() for col in columns: with open(filename) as filein: reader = csv.reader(filein) for row in reader: if col in row: d.append(row) return d