Problem in inserting new key pairs in Dictionary Python

s = "eat" for i in s: d = dict() d[i] = i if i in d: continue else: d[i] = 1 I have tried several times but it is only appending {‘t’: ‘t’} as output, I’m expecting {‘e’: ‘e’, ‘a’: ‘a’, ‘t’, ‘t’} as the output >Solution : You are creating a new dictionary… Read More Problem in inserting new key pairs in Dictionary Python

Create a list of single-entry dictionaries where each group by a given column contributes a value from a 2nd column for all but 1st row which is key

I have a pandas dataframe that looks like this: header1 header2 First row1 Second row2 Third row1 Fourth row2 Fifth row1 I want to create a list of dictionaries where, for all rows with matching value in the header2 column (except the first such row), a dictionary is added to the list using the first… Read More Create a list of single-entry dictionaries where each group by a given column contributes a value from a 2nd column for all but 1st row which is key

How can I find the closest, positive value, from a dictionary's item's keys? (Python)

Assuming I have a dictionary in this structure: data = { ‘111’: {‘a’: ‘xxxxxx’, ‘b’: ‘xxxxxx’, ‘c’: 301}, ‘112’: {‘a’: ‘xxxxxx’, ‘b’: ‘xxxxxx’, ‘c’: 302}, ‘113’: {‘a’: ‘xxxxxx’, ‘b’: ‘xxxxxx’, ‘c’: 377}, ‘114’: {‘a’: ‘xxxxxx’, ‘b’: ‘xxxxxx’, ‘c’: 311}, ‘115’: {‘a’: ‘xxxxxx’, ‘b’: ‘xxxxxx’, ‘c’: 314}, ‘116’: {‘a’: ‘xxxxxx’, ‘b’: ‘xxxxxx’, ‘c’: 306}, ‘117’: {‘a’:… Read More How can I find the closest, positive value, from a dictionary's item's keys? (Python)

combining two dictionary to create a new dictionary in python

I have a dictionary that maps orders to bins. orders_to_bins={1: ‘a’, 2:’a’, 3:’b’} I have another dictionary that shows the items in the orders. items={1:[49,50,51,62], 2:[60,63,64,65], 3:[70,71,72,74]} I need to create a new dictionary that shows the items in the bins like this. items_in_bins={‘a’:[49,50,51,62,60,63,64,65], ‘b’:[70,71,72,74]} I am looking for hints or solutions on how to… Read More combining two dictionary to create a new dictionary in python

Convert dictionary values to floats based on key

I have the following dictionary: {‘Month’: ‘July’, ‘# short’: 8, ‘# cancelled’: 6, ‘% TT delivered’: ‘0.9978408389882788’, ‘% ontime’: ‘0.85284108487160981’, ‘% cancelled’: ‘0.0018507094386181369’, ‘% short’: ‘0.0024676125848241827’, ‘# scheduled’: 3242, ‘# scheduled’: 9697, ‘# ontime’: 8270, ‘Route’: ’82’, ‘Year’: 2005} I want to convert all values where the key starts with a % to floats >Solution… Read More Convert dictionary values to floats based on key

Converting dictionary of lists of dictionaries to a dataframe

Say I have a dict defined as: dict = {‘1’: [{‘name’: ‘Hospital 0’, ‘students’: 5, ‘grad’: 71}, {‘name’: ‘Hospital 1’, ‘students’: 8, ‘grad’: 74}], ‘2’: [{‘name’: ‘Hospital 0’, ‘students’: 11, ‘grad’: 72}] {‘name’: ‘Hospital 1’, ‘students’: 10, ‘grad’: 78}]} Suppose I want to make a dataframe from this formatted as follows: step name students grad… Read More Converting dictionary of lists of dictionaries to a dataframe

Create Dictionary with custom class for value

I’m using a dictionary in C# and want to make the value a custom class. I have the following code. public class myMovies { public string Name { get; set; } public string Year { get; set; } } Dictionary<string, myMovies> file_dict = new Dictionary<string, myMovies>(); foreach (string file in Directory.GetFiles(path1, "*.mkv", SearchOption.AllDirectories)) { file_dict.Add(file,… Read More Create Dictionary with custom class for value