I have a data set that contains lists of pairs of numbers, for instance:
[[12, 1523], [12, 3533], [12, 12], [15, 242], [15, 4243], [15, 15]...]
I have a corresponding dictionary which takes the unique first number in each pair and currently has a blank list associated with the numbers. I.e. this:
dict = {key: [] for key in list}
First couple of examples:
{12: [], 15: [],..}
What I want to do is take all the unique values from the list and put them as values in the dictionary. I.e. I'd like this output:
{12: [1523, 3533], 15: [242, 4243],...}
I was thinking perhaps an if statement. E.g. if the second number within a list pair matches the key, put it in the dictionary. If the value matches the key (e.g. 12 matches the key 12, don't add it to the dictionary). But I'm not sure how to write this in code. Any ideas?
Aucun commentaire:
Enregistrer un commentaire