dictionary

Read .csv file with columns of varying length as dictionary in Python

Read .csv file with columns of varying length as dictionary in Python Question: How do I read in a .csv file in Python with columns of varying lengths? I want to create a dictionary from the .csv file, with the .csv columns as lists of dictionary values. I’ve figured out how to write the dictionary …

Total answers: 3

Check for either key in python dictionary

Check for either key in python dictionary Question: I have a the following code to get some messages from a telegram bot with python: useful_messages = [i["message"] for i in response["result"] if i["message"]["from"]["id"] == int(chat_id) and i["message"]["date"] > last_time] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in …

Total answers: 2

Get dict from list of dicts with the maximum of two items

Get dict from list of dicts with the maximum of two items Question: I would like to get the dict in a list of dicts which has the maximum of two values: manager1={"id":"1","start_date":"2019-08-01","perc":20} manager2={"id":"2","start_date":"2021-08-01","perc":20} manager3={"id":"3","start_date":"2019-08-01","perc":80} manager4={"id":"4","start_date":"2021-08-01","perc":80} managers=[manager1,manager2,manager3,manager4] I want to select the managers that have the latest start date, then get the manager with the …

Total answers: 1

Pandas Dataframe from matrix-like dictionary where keys are tuples of indices

Pandas Dataframe from matrix-like dictionary where keys are tuples of indices Question: I have a dictionary whose keys are tuples of the form (i,j) and whose values are matrix entries. So if you think of a mathematical matrix $A = (a_{i,j})$ then matrix_dict[(i,j)] would give the value of row i and column j. I would …

Total answers: 2

Count dictionary keys and argmax in a list of list

Count dictionary keys and argmax in a list of list Question: Given: import numpy as np list_of_list = [ [‘ccc’, ‘cccc’, ‘b’, ‘c’, ‘b’], [‘ab’, ‘b’, ‘b’, ‘aa’], [‘c’, ‘b’, ‘c’, ‘c’, ‘b’, ‘c’], [‘bb’, ‘d’, ‘c’], ] my_dict = {key: None for key in ‘abcde’} list_of_list is simplified in this test example but it …

Total answers: 1

Extracting the Minimum x Value Keys from Dictionary

Extracting the Minimum x Value Keys from Dictionary Question: Suppose we wish to extract the minimum value from a dictionary like so scores = { 0:1.3399288498085087, 1:1.2672683347433629, 3:1.6999159970296505, 4:1.8410942584597279, 5:1.336658057628646 } #find minimum value in dictionary minimum_value = min(scores.values()) #get keys with minimal value using list comprehension minimum_keys = [key for key in scores if …

Total answers: 3

String variable not behaving correctly in json.dumps

String variable not behaving correctly in json.dumps Question: I’ve got an API request I am sending and I gather the necessary data and compile it in the proper format into a string called compiled_string. When I try to include it in json.dumps, it is being changed so that my API request is not working properly. …

Total answers: 1

Find timestamp difference in dictionary values within list of dictionary and capture dictionary with max difference to previous

Find timestamp difference in dictionary values within list of dictionary and capture dictionary with max difference to previous Question: I have a list of dictionary like below : [ {‘dt’: ‘2023-04-08T18:04:33.476+00:00’, ‘msg’: ‘VWX’ }, {‘dt’: ‘2023-04-08T18:04:16.814+00:00’, ‘msg’: ‘STU’ }, {‘dt’: ‘2023-04-08T18:01:40.504+00:00’, ‘msg’: ‘PQR’ }, {‘dt’: ‘2023-04-08T18:01:40.345+00:00’, ‘msg’: ‘MNO’ }, {‘dt’: ‘2023-04-08T18:01:40.068+00:00’, ‘msg’: ‘JKL’ }, {‘dt’: …

Total answers: 2