In your current code, what Dictionary.update() does is that it updates (update means the value is overwritten from the value for same key in passed in dictionary) the keys in current dictionary with the values from the dictionary passed in as the parameter to it (adding any new key:value pairs if existing) . A single flat dictionary does not satisfy your requirement , you either need a list of dictionaries or a dictionary with nested dictionaries.
If you want a list of dictionaries (where each element in the list would be a diciotnary of a entry) then you can make case_list as a list and then append case to it (instead of update) .
Example -
case_list = []
for entry in entries_list:
case = {'key1': entry[0], 'key2': entry[1], 'key3':entry[2] }
case_list.append(case)
Or you can also have a dictionary of dictionaries with the key of each element in the dictionary being entry1 or entry2 , etc and the value being the corresponding dictionary for that entry.
case_list = {}
for entry in entries_list:
case = {'key1': value, 'key2': value, 'key3':value }
case_list[entryname] = case #you will need to come up with the logic to get the entryname.
Answer from Anand S Kumar on Stack OverflowIn your current code, what Dictionary.update() does is that it updates (update means the value is overwritten from the value for same key in passed in dictionary) the keys in current dictionary with the values from the dictionary passed in as the parameter to it (adding any new key:value pairs if existing) . A single flat dictionary does not satisfy your requirement , you either need a list of dictionaries or a dictionary with nested dictionaries.
If you want a list of dictionaries (where each element in the list would be a diciotnary of a entry) then you can make case_list as a list and then append case to it (instead of update) .
Example -
case_list = []
for entry in entries_list:
case = {'key1': entry[0], 'key2': entry[1], 'key3':entry[2] }
case_list.append(case)
Or you can also have a dictionary of dictionaries with the key of each element in the dictionary being entry1 or entry2 , etc and the value being the corresponding dictionary for that entry.
case_list = {}
for entry in entries_list:
case = {'key1': value, 'key2': value, 'key3':value }
case_list[entryname] = case #you will need to come up with the logic to get the entryname.
As per my understanding you want data in dictionary as shown below:
key1: value1-1,value1-2,value1-3....value100-1
key2: value2-1,value2-2,value2-3....value100-2
key3: value3-1,value3-2,value3-2....value100-3
for this you can use list for each dictionary keys:
case_list = {}
for entry in entries_list:
if key in case_list:
case_list[key1].append(value)
else:
case_list[key1] = [value]
Videos
Hello, I'm trying to compare the kappa scores for one person against other people. I want to generate a dataframe that has the kappa for person A vs person B, person A vs person C, person A vs person D, etc.
To do this, I was thinking of reading in a everyone's scores that they gave as a dataframe, and assigning them to a dictionary. So something like {person B : person_B_scores_df, person C: person_C_scores}
But I can't figure out how to iterate over a for loop and add the key pairings to a dictionary. I keep getting a key error. Here is my code.
## Create list of raters
rater_list = ["person_A", "person_B", "person_C", "person_D"]
rater_dict = {}
## Pull in each rater's scores and add them key/value pairing in rater_dict
for name in rater_list:
rater_df = pd.read_csv(input_path/f"rater_scores_{name}.csv")
rater_dict[name].append(rater_df)
## Also tried the following!! - The code below only appends the last person in the rater list to the dictionary
#for name in rater_list:
# rater_df = pd.read_csv(input_path/f"rater_scores_{name}.csv")
# rater_dict={name:rater_df}You don't really need the loop at all, you can just use the in keyword to check the name against the existing keys in the dictionary.
phoneBook = dict()
def addNumber(name, number):
if name in phoneBook:
print 'error'
else:
phoneBook[name] = number
why bother
people = ((data["name"],data["number"]) for data in json.loads(some_list)[::-1])
phoneBook = dict(people)
this will run in reverse order through the list so the first occurrence of the name will be the one stored in the dictionary
it takes longer to check than to just insert it ... even if it ends up over written
to answer your question however
your original code did not work because your else was inside the forloop essentially adding the entry if any name in the book did not match the name being inserted
you can fix it easily enough, by exiting the function when a match is found... and not adding the name to the dictionary until you have checked all names
phoneBook = dict()
def addNumber(name, number):
for i in phoneBook:
if i == name:
print 'error'
return
phoneBook[name] = number
addNumber("james",56)
addNumber("Tommy",26)
addNumber("james",77)
I am currently using a while loop to loop and get certain values. I then want to append these values to a dictionary. Every loop through I want to append to two keys
General structure of the code: https://pastebin.com/p4hJcKR5
I have tried using:
dict[key] = value
dict.append(value)
And neither have worked, dict.append gives an error and dict[key] just sets the dictionary to the most recent iteration instead of iterating for all values. Any help would be appreciated.