I have a dictionary containing keys that at the same time the key contains a certain list of integers. I want to built a dict that shows me the values as keys and the values of this new dict would be a list of the previous keys that contained the previous value. For example, I have:
turns
{'Monday_2022_03_14': [25, 17, 2, 32, 43, 40, 23, 7, 19, 16],
'Tuesday_2022_03_15': [33, 10, 16, 31, 25, 22, 43, 41, 21, 28, 3, 30, 29],
'Wednesday_2022_03_16': [19, 27, 40, 3, 9, 2, 14, 21, 44, 17],
'Thursday_2022_03_17': [16, 23, 1, 30, 29, 44, 5, 42, 27, 19],
'Friday_2022_03_18': [6, 17, 2, 29, 27, 41, 44, 5, 40, 42]}
I would like to have, as an example:
{1:['Thursday_2022_03_17'],
2:['Monday_2022_03_14','Wednesday_2022_03_16','Friday_2022_03_18'],
3:['Tuesday_2022_03_15','Wednesday_2022_03_16],
...} #and so on
I tried to implement the next solution on a loop (I found it in Get key by value in dictionary), but then I realized this could work only in dicts with keys of only one value, not lists like mine:
list(turns.keys())[list(turns.values()).index(16)]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_19168/930684082.py in <module>
----> 1 list(turns.keys())[list(turns.values()).index(16)]
ValueError: 16 is not in list
It's ok if you don'y provide a loop solution, I can implement it but I need help in getting the key name by value (like the line above), but focusing on a list instead of a simple value.
Thanks to all.
CodePudding user response:
One way is to use dict.setdefault
and iterative over the lists:
out = {}
for k, lst in turns.items():
for v in lst:
out.setdefault(v, []).append(k)
Then if you want the dictionary sorted by key, you could use sorted
:
out = {k: out[k] for k in sorted(out)}
Output:
{1: ['Thursday_2022_03_17'],
2: ['Monday_2022_03_14', 'Wednesday_2022_03_16', 'Friday_2022_03_18'],
3: ['Tuesday_2022_03_15', 'Wednesday_2022_03_16'],
5: ['Thursday_2022_03_17', 'Friday_2022_03_18'],
6: ['Friday_2022_03_18'],
7: ['Monday_2022_03_14'],
9: ['Wednesday_2022_03_16'],
10: ['Tuesday_2022_03_15'],
14: ['Wednesday_2022_03_16'],
16: ['Monday_2022_03_14', 'Tuesday_2022_03_15', 'Thursday_2022_03_17'],
17: ['Monday_2022_03_14', 'Wednesday_2022_03_16', 'Friday_2022_03_18'],
19: ['Monday_2022_03_14', 'Wednesday_2022_03_16', 'Thursday_2022_03_17'],
21: ['Tuesday_2022_03_15', 'Wednesday_2022_03_16'],
22: ['Tuesday_2022_03_15'],
23: ['Monday_2022_03_14', 'Thursday_2022_03_17'],
25: ['Monday_2022_03_14', 'Tuesday_2022_03_15'],
27: ['Wednesday_2022_03_16', 'Thursday_2022_03_17', 'Friday_2022_03_18'],
28: ['Tuesday_2022_03_15'],
29: ['Tuesday_2022_03_15', 'Thursday_2022_03_17', 'Friday_2022_03_18'],
30: ['Tuesday_2022_03_15', 'Thursday_2022_03_17'],
31: ['Tuesday_2022_03_15'],
32: ['Monday_2022_03_14'],
33: ['Tuesday_2022_03_15'],
40: ['Monday_2022_03_14', 'Wednesday_2022_03_16', 'Friday_2022_03_18'],
41: ['Tuesday_2022_03_15', 'Friday_2022_03_18'],
42: ['Thursday_2022_03_17', 'Friday_2022_03_18'],
43: ['Monday_2022_03_14', 'Tuesday_2022_03_15'],
44: ['Wednesday_2022_03_16', 'Thursday_2022_03_17', 'Friday_2022_03_18']}