dictionary

Handle self-references when flattening dictionary

谁说我不能喝 提交于 2021-01-28 10:53:52
问题 Given some arbitrary dictionary mydict = { 'first': { 'second': { 'third': { 'fourth': 'the end' } } } } I've written a small routine to flatten it in the process of writing an answer to another question. def recursive_flatten(mydict): d = {} for k, v in mydict.items(): if isinstance(v, dict): for k2, v2 in recursive_flatten(v).items(): d[k + '.' + k2] = v2 else: d[k] = v return d It works, giving me what I want: new_dict = recursive_flatten(mydict) print(new_dict) {'first.second.third.fourth

How to enumerate items in a dictionary with enumerate( ) in python

孤街浪徒 提交于 2021-01-28 09:36:54
问题 As the title suggests I wanted to enumerate the key and its values (without brackets) in python. I tried the following code : example_dict = {'left':'<','right':'>','up':'^','down':'v',} [print(i,j,a) for (i,j,a) in enumerate(example_dict.items())] But it doesn't work. I want the output to be like this 0 left < 1 right > 2 up ^ 3 down v Thank you in advance 回答1: In this case enumerate returns (index, (key, value)) , so you just need to change your unpacking to for i, (j, a) , though

pandas - How to load data from nested dictionary into dataframe?

一曲冷凌霜 提交于 2021-01-28 08:10:52
问题 I'm tryping to create a dataframe with closingprices for stocks and have found a free api that returns JSON-data in the form of nested dicts with the data, looking like this: {'name': 'AAPL', 'history': {'2019-01-04': {'open': '144.53', 'close': '148.26', 'high': '148.55', 'low': '143.80', 'volume': '58607070'}, '2019-01-03': {'open': '143.98', 'close': '142.19', 'high': '145.72', 'low': '142.00', 'volume': '91312195'}, '2019-01-02': {'open': '154.89', 'close': '157.92', 'high': '158.85',

Replacing a value in string with a value in a dictionary in python

依然范特西╮ 提交于 2021-01-28 06:51:26
问题 Could you assist me with replacing an identifier value with a value from a dictionary. So the code looks like follow #string holds the value we want to output s = '${1}_p${guid}s_${2}' d = {1: 'bob', 2: '123abc', 3: 'CA', 4: 'smith' } I want to be able to replace ${1} with bob and ${2} with 123abc, I only want to change a value if the value inside ${} is just a number then, replace it with the value within the dictionary. output = 'bob_p${guid}s_123abc' I tried using template module, but it

Sort a dictionary by values in ascending order and by keys in descending order

你。 提交于 2021-01-28 06:27:42
问题 I am trying to sort a dictionary, the order which I want to follow is that first, the dictionary should be sorted in increasing order by values and if the values for two or more keys are equal then I want to sort the dictionary by the keys in descending order. Here is the code: dictionary = {0: 150, 1: 151, 2: 150, 3: 101, 4: 107} print(sorted(dictionary.items(), key=lambda x: (x[1], x[0]))) I want the output to be the following: [(3, 101), (4, 107), (2, 150), (0, 150), (1, 151)] But the

How to find Duplicate values in Dict and print keys with those values

倖福魔咒の 提交于 2021-01-28 06:20:28
问题 I wanted to know how I can find duplicate values in a dictionary and the return the keys that contain those values. So here is an example: d = {'happy':['sun', 'moon', 'chocolate'], 'sad':['fail', 'test', 'bills'], 'random': ['baloon', 'france', 'sun'] } As you can see the key's happy and random have the same/duplicate value in them, which is 'sun' , so the output I was looking for is: random, happy I cant really understand how I can find duplicate values like that. If I had a particular

Get dictionary name by string in C#

喜夏-厌秋 提交于 2021-01-28 06:10:34
问题 I have a class which contains 4 dictionaries. public class FishInformation { public int FishId { get; set; } public Dictionary<string,int> DicGroup { get; set; } public Dictionary<string, int> DicEvent { get; set; } public Dictionary<string, int> DicAudience { get; set; } public Dictionary<string, int> DicType { get; set; } } I want get the dictionary name by a string and add items to it. So this question suggests using System.Reflection to do so. this is the code I tried: FishInformation

Pythonic Way To Replace Dict Keys and/or Values

非 Y 不嫁゛ 提交于 2021-01-28 06:01:53
问题 I'm looking for an 'efficient' way to iterate through a dictionary, and replace any key or value that starts with the term 'var'. For example, if I have this: data = { "user_id": "{{var_user_id}}", "name": "bob", "{{var_key_name}}": 4 } and I have this dict of variable values: variables = { "user_id": 10, "key_name": "orders_count" } Then I'd like my final data dict to look like this: data = { "user_id": 10, "name": "bob", "orders_count": 4 } 回答1: Since you're treating it like a text template

Parsing text file in Python into a dictionary

喜夏-厌秋 提交于 2021-01-28 05:43:48
问题 I have a csv file. I want to create dictionary from this data.I should not pandas. Data looks like this: I do this. But size of the numbers is not same. How can I create a dictionary? filename=" data.dat" file=open(filename, encoding="latin-1").read().split(' , ') dictt={} for row in file: dictt[row[0]] = {‘values’, row[1]} I have a file as above. First, I need to create a dict. After that, I will print the daily number of unique measurements in desending order according to date. Final

Python - Remove dicts with empty values from list of dictionaries

牧云@^-^@ 提交于 2021-01-28 05:32:02
问题 I'd like to remove the list of dictionaries elements that have empty values. So for the following: dict = [{"end": "00:15", "item": "Paul Test 1", "start": "00:00"}, {"end": "00:14", "item": "Paul Test 2 ", "start": "00:30"}, {"end": "", "item": "", "start": ""}, {"end": "", "item": "", "start": ""}, {"end": "", "item": "", "start": ""}] would return a new dict of: [{"end": "00:15", "item": "Paul Test 1", "start": "00:00"}, {"end": "00:14", "item": "Paul Test 2 ", "start": "00:30"}] I've