我有2个字典,并希望连接他们的关键字,输出字典应该只有两个字典的值
counter = Counter({0: 1702,
1: 1920,
2: 1851,
3: 1882,
4: 1745,
5: 1741,
6: 1827,
7: 1961,
8: 1790,
9: 1926})
labels =
{0: 'Tomato___Bacterial_spot',
1: 'Tomato___Early_blight',
2: 'Tomato___Late_blight',
3: 'Tomato___Leaf_Mold',
4: 'Tomato___Septoria_leaf_spot',
5: 'Tomato___Spider_mites Two-spotted_spider_mite',
6: 'Tomato___Target_Spot',
7: 'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
8: 'Tomato___Tomato_mosaic_virus',
9: 'Tomato___healthy'}输出
"Tomato___Bacterial_spot": 1920,
"Tomato___healthy": 1926,发布于 2021-05-23 15:38:06
from collections import Counter
counter = Counter({0: 1702,
1: 1920,
2: 1851,
3: 1882,
4: 1745,
5: 1741,
6: 1827,
7: 1961,
8: 1790,
9: 1926})
labels = {0: 'Tomato___Bacterial_spot',
1: 'Tomato___Early_blight',
2: 'Tomato___Late_blight',
3: 'Tomato___Leaf_Mold',
4: 'Tomato___Septoria_leaf_spot',
5: 'Tomato___Spider_mites Two-spotted_spider_mite',
6: 'Tomato___Target_Spot',
7: 'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
8: 'Tomato___Tomato_mosaic_virus',
9: 'Tomato___healthy'}
res = {}
for k, v in labels.items():
res[v] = counter[k]
print(res)或者类似地,您可以使用字典理解:
res2 = {v: counter[k] for k, v in labels.items()}
print(res2)https://stackoverflow.com/questions/67657270
复制相似问题