在列表字典中按值获取键 -python
Get key by value in dictionary of lists -python
我有一个包含键的字典,同时键包含特定的整数列表。我想构建一个字典,将值显示为键,而这个新字典的值将是包含先前值的先前键的列表。例如,我有:
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]}
我想举个例子:
{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
我试图在循环中实现下一个解决方案(我在 Get key by value in dictionary 中找到了它),但后来我意识到这只能在只有一个值的键的字典中工作,而不是像我这样的列表:
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
如果你不提供循环解决方案也没关系,我可以实现它,但我需要帮助按值获取键名(如上面的行),但重点是列表而不是简单的值。
感谢大家。
一种方法是使用 dict.setdefault
并迭代列表:
out = {}
for k, lst in turns.items():
for v in lst:
out.setdefault(v, []).append(k)
然后如果你想要字典按键排序,你可以使用sorted
:
out = {k: out[k] for k in sorted(out)}
输出:
{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']}
我有一个包含键的字典,同时键包含特定的整数列表。我想构建一个字典,将值显示为键,而这个新字典的值将是包含先前值的先前键的列表。例如,我有:
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]}
我想举个例子:
{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
我试图在循环中实现下一个解决方案(我在 Get key by value in dictionary 中找到了它),但后来我意识到这只能在只有一个值的键的字典中工作,而不是像我这样的列表:
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
如果你不提供循环解决方案也没关系,我可以实现它,但我需要帮助按值获取键名(如上面的行),但重点是列表而不是简单的值。
感谢大家。
一种方法是使用 dict.setdefault
并迭代列表:
out = {}
for k, lst in turns.items():
for v in lst:
out.setdefault(v, []).append(k)
然后如果你想要字典按键排序,你可以使用sorted
:
out = {k: out[k] for k in sorted(out)}
输出:
{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']}