dictionary

C#: Bind Dictionary<string, List<string>> to DataTable

[亡魂溺海] 提交于 2021-02-10 20:01:11
问题 I have one dictionary Like Dictionary<string, List<string>> first=new Dictionary<string, List<string>>(); I want to bind this dictionary to data table such that data table ColumnName should be key of the dictionary and respective columns should contain their dictionary values. What I tried: Dictionary<string, List<string>> some= new Dictionary<string, List<string>>(); System.Data.DataTable dt = new System.Data.DataTable(); foreach (var entry in some) { if (entry.Value.Count > 0) { dt.Columns

Why “Map” manipulation is much slower than “Object” in JavaScript (v8) for integer keys?

不想你离开。 提交于 2021-02-10 17:48:01
问题 I was happily using Map for indexed accessed everywhere in my JavaScript codebase, but I've just stumbled upon this benchmark: https://stackoverflow.com/a/54385459/365104 I've re-created it here as well: https://jsben.ch/HOU3g What benchmark is doing is basically filling a map with 1M elements, then iterating over them. I'd expect the results for Map and Object to be on par, but they differ drastically - in favor of Object. Is this expected behavior? Can it be explained? Is it because of the

Flatten nested dictionaries with tuple keys

烈酒焚心 提交于 2021-02-10 17:47:34
问题 How to generalize this question to the case keys that may be tuples? As a benefit even in the case of all string keys, if these are accumulated to a tuple, there's no need for ad-hoc separators (though JSON export is another matter): one approach is to base it on this answer. I tried 2 versions: def flatten_keys(d,handler,prefix=[]): return {handler(prefix,k) if prefix else k : v for kk, vv in d.items() for k, v in flatten_keys(vv, handler, kk).items() } if isinstance(d, dict) else { prefix :

Find the most common element in list of lists

一曲冷凌霜 提交于 2021-02-10 17:44:09
问题 This is my list a=[ ['a','b','a','a'], ['c','c','c','d','d','d']] I wanna find most common elemments. I have tried this from collections import Counter words = ['hello', 'hell', 'owl', 'hello', 'world', 'war', 'hello', 'war','aa','aa','aa','aa'] counter_obj = Counter(words) counter_obj.most_common() but it works just for simple list. my output should be like this [('a', 3), ('c', 3), ('d', 3), ('b', 1)] 回答1: Apply Counter().update() option on the elements of your list, Based on suggestion

How to append several dictionaries while looping through pagination (API) - python 3

白昼怎懂夜的黑 提交于 2021-02-10 16:51:09
问题 I would like to loop through several pages of data which I am accessing through an API, and concatenate the pages to the same dictionary as the loop progresses. So far I managed to loop through all the pages and could print out as the loop goes on, so when the loop is over, all the pages are printed out. But my dictionary contains only the last page! page = 5 i = 0 for i in range(0, page): url =f'http://hotell.difi.no/api/json/npd/wellbore/withcoordinates?page={i+1}' dataset_all = requests

Creating nested dictionaries from a list containing paths

空扰寡人 提交于 2021-02-10 14:50:49
问题 I have a list containing paths. For example: links=['main', 'main/path1', 'main/path1/path2', 'main/path1/path2/path3/path4', 'main/path1/path2/path3/path5', 'main/path1/path2/path3/path4/path6'] I want to create a nested dictionary to store these paths in order. Expected output: Output = {‘main’: {‘path1’: {‘path2’: {‘path3’: {‘path4’: {‘path6’: {} }},‘path5’:{}}}}} I am new to python coding (v 3.+) and I am unable to solve it. It gets confusing after i reach path 3 as there is path 4 (with

Creating nested dictionaries from a list containing paths

喜你入骨 提交于 2021-02-10 14:50:26
问题 I have a list containing paths. For example: links=['main', 'main/path1', 'main/path1/path2', 'main/path1/path2/path3/path4', 'main/path1/path2/path3/path5', 'main/path1/path2/path3/path4/path6'] I want to create a nested dictionary to store these paths in order. Expected output: Output = {‘main’: {‘path1’: {‘path2’: {‘path3’: {‘path4’: {‘path6’: {} }},‘path5’:{}}}}} I am new to python coding (v 3.+) and I am unable to solve it. It gets confusing after i reach path 3 as there is path 4 (with

pandas mapping list to dict items for new column

久未见 提交于 2021-02-10 14:41:05
问题 i have df like: col_A [1,2,3] [2,3] [1,3] and dict like: dd = {1: "Soccer", 2: "Cricket", 3: "Hockey"} how can i create a new column col_B like: col_A col_B [1,2,3] ["Soccer", "Cricket", "Hockey"] [2,3] ["Cricket", "Hockey"] [1,3] ["Soccer", "Hockey"] tried something like: df['sports'] = df['col_A'].map(dd) got error: TypeError: unhashable type: 'list' 回答1: You can use list comprehension with if for filter out not matched values: df['sports'] = df['col_A'].map(lambda x: [dd[y] for y in x if y

Replace values in dict if value of another key within a nested dict is found in value

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-10 14:23:01
问题 I have the following nested dict: ex_dict = {'path1': {'$variable1': '2018-01-01', '$variable2': '2020-01-01', '$variable3': '$variable1', '$variable4': '$variable3'}, 'path2': {'$variable1': '2018-01-01', '$variable2': '2020-01-01', '$variable3': '$variable1', '$variable4': '$variable1 + $variable2'} } I want to replace any $variableX specified for a dict key with the dict value from another key if the key from the other dict value if found in the value of the original dict key. See example

List of dict of dict in Pandas

可紊 提交于 2021-02-10 14:18:19
问题 I have list of dict of dicts in the following form: [{0:{'city':'newyork', 'name':'John', 'age':'30'}}, {0:{'city':'newyork', 'name':'John', 'age':'30'}},] I want to create pandas DataFrame in the following form: city name age newyork John 30 newyork John 30 Tried a lot but without any success can you help me? 回答1: Use list comprehension with concat and DataFrame.from_dict: L = [{0:{'city':'newyork', 'name':'John', 'age':'30'}}, {0:{'city':'newyork', 'name':'John', 'age':'30'}}] df = pd