我有一本看起来像这样的字典:
Counter({'firstKey': 1708, 'secondKey': 1589, 'thirdKey': 1424})
我想要两个列表:
第一个列表应该列出所有键:
['firstKey', 'secondKey', 'thirdKey']
第二个列表应列出所有值:
[1708, 1589, 1424]
请帮忙
最佳答案
尝试 list(Counter.keys())
and list(Counter.values())
>>> counter= Counter({'firstKey': 1708, 'secondKey': 1589, 'thirdKey': 1424})
>>> Keys = list(counter.keys())
>>> Keys
['firstKey', 'secondKey', 'thirdKey']
>>> Vals = list(counter.values())
>>> Vals
[1708, 1589, 1424]
>>>
关于Python 字典 : Create a list for all keys in dict, 另一个用于字典中的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61426140/