我有一本以下格式的字典。我已确保键按排序顺序排列,这些键的值也是如此{67: ['226', '2470', '2517', '2578', '2799', '281', '2905', '3015', '3081', '347', '411'], 68: ['276', '331', '387', '442', '497', '607', '662']}现在,我想为 ak,v 对的每个组合分配一个自动递增整数。例子:{(67, 226):1, (67, 2470): 2 ... (68, 662):18}
2 回答
一只斗牛犬
TA贡献1784条经验 获得超2个赞
假设输入字典被称为d:
from itertools import count
ls = [(k, n) for k, v in d.items() for n in v]
result = dict(zip(ls, count(1)))
慕的地8271018
TA贡献1796条经验 获得超4个赞
我想到这个来解决你的问题
data = {67: ['226', '2470', '2517', '2578', '2799', '281', '2905', '3015', '3081', '347', '411'], 68: ['276', '331', '387', '442', '497', '607', '662']}
cont = 1
output_dict = {}
for d in data:
for value_d in data[d]:
output_dict[(d, value_d)] = cont
cont += 1
print(output_dict)
添加回答
举报
0/150
提交
取消