dictionary

Filtering dictionary elements by few initial values

Filtering dictionary elements by few initial values Question: I have the following data structure: Clients= { "data": [ { "nClients": 3 }, { "name": "Mark", "roll_no": 1, "branch": "c" }, { "name": "Cris", "roll_no": 3, "branch": "it3" }, { "name": "Mark", "roll_no": 2, "branch": "it2" } ] } I am trying to figure out a …

Total answers: 2

Problems with reciprocal of an dictionary

Problems with reciprocal of an dictionary Question: given dict: weights = {"A":16, "B": 3, "C": 5) I want to have the reciprocal of the values. Output: weights_dict_reci = {"A":0,0625, "B": 0,3333333, "C": 0,2) So far I tried: weights_dict_reci = {value: 1 / weights_dict[value] for value in weights_dict} and for key in weights_dict: weights_dict[key] = 1 …

Total answers: 2

Python: Iterate and modify a complex dict including lists

Python: Iterate and modify a complex dict including lists Question: I need to iterate through a complex dictionary and modify it according to several conditions. This is a very basic example of the dict a = { ‘x’ : ["x1", "x2", ‘x3’], ‘y’ : [ {‘y1’ : 1}, {‘y2’ : 2.0}, {‘y3’ : True}, {‘y4’ …

Total answers: 2

Convert list to a sequence of dictionary keys

Convert list to a sequence of dictionary keys Question: I have 2 dictionaries old_dict = {‘key1’: {‘password1’: ‘password’, ‘sensitive_data’: {‘MY_PASSWORD’: ‘old_passwrd’}}, ‘key2’: ‘next_password’, ‘useless_key’: ‘useless_pass’} new_dict = {‘key1’: {‘password1’: ‘password’, ‘sensitive_data’: {‘MY_PASSWORD’: ‘default_password’}}, ‘key2’: ‘next_password’} Next code parses the keys in the new dictionary and takes values ​​from the old one and inserts them into …

Total answers: 2

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