I'm trying to select all of the key,values from one dictionary (imgs_dict) that have a key that appears in another dictionary (train_data_labels) and save the result into a third dictionary (training_images). One of the two original dictionaries has keys that are stored as numeric values (train_data_labels) and one is stored as a string (imgs_dict), so I first try to convert the key values of train_data_labels to a string and add leading zeros to match structure of imgs_dict, but this creates a list rather than a dictionary.
# Add leading zeros and convert key values to strings to allow referencing the image dictionary
training_ids=[str(k).zfill(5) for k in train_data_label]
After this I am trying to pull the image ids appearing in training_ids from imgs_dict, this appears to work but creates a list rather than a dictionary.
# Pull the training images from the total image set and save in a training images dictionary
training_images=[imgs_dict[i] for i in training_ids if i in imgs_dict]
It seems like there should be a straight forward way to save a new dictionary with altered keys, then select certain dictionary items that you want from another dictionary based on another list or dictionary but I'm having trouble finding it.
You could use a dictionary comprehension instead of the second list comp.
training_images = {key: imgs_dict[key] for key in training_ids if key in imgs_dict}
Related
So I have a dictionary names "ngrams_count". I want to find all keys in this dictionary that are in a list called "words_to_find". I would also like to return the values associated with those keys.
So far, this is what I'm working with
ideasrep = [key for key in words_to_find if key in ngrams_count]
That returns only the keys that are found in the word list.
I'm also looking for a way to return only the key/values pairs for which the value is greater than one. I've tried a similar technique as this:
[(key,values) for key, values in ngrams_count.items() if values > 1]
However, this only seems to work if I stay within the dictionary and I'm running out of ideas... Ideally, I'd like a way to do these two things simultaneously.
Your first version is almost right, you just need to add ngrams_count[key] to the result.
ideasrep = [(key, ngrams_count[key]) for key in words_to_find if key in ngrams_count]
Or you can use the second version, but change the condition to check if the key is in words_to_find.
[(key,values) for key, values in ngrams_count.items() if key in words_to_find]
If words_to_find is big, you should convert it to a set before the list comprehension to make the second version more efficient.
I have an iterator for creating multiple lists. I need to keep adding the generated list to a dictionary dict1 based on the key value k:
some value here = k
for a in jsoncontent:
list1.append(a["Value"])
dict1.setdefault(k, []).append(list1)
Right now I get:
{k:[[10,11],[12,32,6],[7,4]]}
But I need:
{k:[10,11,12,32,6,7,4]}
How do I merge these lists?
It sounds like you want extend versus append. extend inserts the contents of the list at the end of the list, while append insets its argument, a list in this case. See https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types
I have a dictionary with unique values and I want to invert it (i.e. swap keys with values) inplace.
Is there any way doing it without using another dictionary?
I would prefer to just manipulate the items inside the dict than use a new dictionary, so that id(my_dict) would remain the same.
If you are trying to swap keys and values and do not mind duplicate values creating key conflicts, you can "reverse" the dictionary rather easily with a single line of code:
dictionary = dict(map(reversed, dictionary.items()))
If you would rather use the dictionary comprehension syntax instead, you can write this line:
dictionary = {value: key for key, value in dictionary.items()}
If you do not want to use the items method of the dictionary, it is rather easy to avoid:
dictionary = {dictionary[key]: key for key in dictionary}
If you can afford creating a copy of the dictionary, you can reverse it in place if needed:
def reverse_in_place(dictionary):
reference = dictionary.copy()
dictionary.clear()
dictionary.update(map(reversed, reference.items()))
I guess you want to swap the keys and the values of the dict?
You can do it like this:
dict_name = dict(zip(dict_name.values(), dict_name.keys()))
I am converting lists to dictionary...the dictionary may contain duplicate key value pairs. Python Dictionary returns key value pairs based on the last occurrence in the input data. How can i take first key-value pairs from the dictionary when a key repeats multiple times?
i see one solution as mentioned in following link, i need to create list of values for key and then take first element from the values list.
List of values for duplicate keys in dictionary Python
Is there any other better way to do this?
You can just iterate through your list backwards using reversed when constructing the dictionary. The constructed dictionary will then only contain the first values of the original list.
I need a little assistance with inserting a key:value pair from a dictionary (called umDict) into an already existing dictionary in a list at a specific spot in that list.
What I have already is a list (called rLu) that's filled with 943 empty dictionaries. I also have a list called lineList that has parsed a string and contains three elements. Finally, I have a key:value pair in the umDict temporary dictionary that needs to be inserted into the dictionary that's present in the rLu list at
rLu[int(lineList[0])-1]
The key value pair is composed such as:
umDict[lineList[1]] = lineList[2]
To get the results I'd like, I've tried:
umDict[lineList[1]] = lineList[2]
rLu[int(lineList[0])-1] = umDict
But it inserts a brand new dictionary instead of just the key:value into the existing dictionary. How do I get it to insert just the key:value pair into the existing dictionary?
What I'm expecting to get is something like this:
rLu = [{'1':'a','2':'b'}, {'3':'c', '4':'d'}, {'5':'e', '6':'f'}]
Where the length of those dictionaries could be any, not just two like in my example.
Perhaps you want rLu[int(lineList[0])-1].update(umDict)? It's a little difficult to understand your data structure from your description. If I understand you right, you do not have a "key-value pair", you have a one-key dictionary.