dictionary

Python create parse a dictionary to another format

Python create parse a dictionary to another format Question: I wanted to parse a dictionary with a format. Currently this dictionary has the same user ID in separate dictionaries and I wanted to make it same user ID in one dictionary with key and values. Below is my current dictionary: { "key1": { "userID1": 5, …

Total answers: 2

Convert list of dictionaries to dictionary of dictionaries

Convert list of dictionaries to dictionary of dictionaries Question: I have a list of dictionaries ls1 = [{‘AccountBalance’: ’78’, ‘Status’: ‘0’}, {‘AccountBalance’: ’56’, ‘Status’: ‘1’}, {‘AccountBalance’: ’34’, ‘Status’: ‘0’}, {‘AccountBalance’: ’12’, ‘Status’: ‘0’}] I would like to convert this to a dictionary of 2 dictionaries dict1 = {‘AccountBalance’: {0: ’78’, 1: ’56’, 2: ’34’, 3: …

Total answers: 4

How to write to dictionary using user input and loop and parsing by space?

How to write to dictionary using user input and loop and parsing by space? Question: I am trying to create a dictionary that I can serialize into a json file. This is my first time trying this so I am very confused. I want user input for a patient’s first name, last name, age, and …

Total answers: 3

How to pad 0s to garantee same length while iterating over two lists?

How to pad 0s to garantee same length while iterating over two lists? Question: My inputs are : x = [‘A’, ‘B’, ‘B’, ‘C’, ‘D’, ‘D’, ‘D’] y = [ 4 , 3 , 5 , 9 , 1 , 6 , 2 ] And I’m just trying to make this dictionnary. {‘A’: [4, 0, …

Total answers: 2

Question about data_collator throwing a key error in Hugging face

Question about data_collator throwing a key error in Hugging face Question: I am trying to use data_collator function in hugging face using this code: datasets = dataset.train_test_split(test_size=0.1) train_dataset = datasets["train"] val_dataset = datasets["test"] print(type(train_dataset)) def data_collator(data): # Initialize lists to store pixel values and input ids pixel_values_list = [] input_ids_list = [] # Iterate over …

Total answers: 1

Process the python dictionary to remove undesired elements and retain desired ones

Process the python dictionary to remove undesired elements and retain desired ones Question: I have a python dictionary as given below: ip = { "doc1.pdf": { "img1.png": ("FP", "text1"), "img2.png": ("NP", "text2"), "img3.png": ("FP", "text3"), }, "doc2.pdf": { "img1.png": ("FP", "text4"), "img2.png": ("NP", "text5"), "img3.png": ("NP", "text6"), "img4.png": ("NP", "text7"), "img5.png": ("Others", "text8"), "img6.png": ("FP", …

Total answers: 3

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