简体   繁体   English

Python 字典:为字典中的所有键创建一个列表,为字典中的所有值创建另一个列表

[英]Python Dictionary: Create a list for all keys in dict, another for all values in dict

I have a dictionary which looks like this: Counter({'firstKey': 1708, 'secondKey': 1589, 'thirdKey': 1424})我有一个看起来像这样的字典: Counter({'firstKey': 1708, 'secondKey': 1589, 'thirdKey': 1424})

I would like to have two lists:我想要两个列表:

First list should list all keys: ['firstKey', 'secondKey', 'thirdKey']第一个列表应该列出所有键: ['firstKey', 'secondKey', 'thirdKey']

Second list should list all values: [1708, 1589, 1424]第二个列表应列出所有值: [1708, 1589, 1424]

Please help请帮忙

Usingdict.keys() and dict.values() and casting them to list :使用dict.keys()dict.values()并将它们转换为list

d = {'firstKey': 1708, 'secondKey': 1589, 'thirdKey': 1424}

print(list(d.keys()))
print(list(d.values()))

OUTPUT: OUTPUT:

['firstKey', 'thirdKey', 'secondKey']                                                                                                                          
[1708, 1424, 1589]

Try list(dict.keys()) and list(Counter.values())尝试list(dict.keys())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]
>>> 

Cast list() to the dictionary to get the keys in a list, and cast list() to the dict.values() to get the values in a list:list()转换为字典以获取列表中的键,并将list()转换为dict.values()以获取列表中的值:

>>> from collections import Counter
>>> d = Counter({'firstKey': 1708, 'secondKey': 1589, 'thirdKey': 1424})
>>> l1 = list(d)
>>> l1
['firstKey', 'secondKey', 'thirdKey']
>>> l2 = list(d.values())
>>> l2
[1708, 1589, 1424]

Could also usedict.keys() :也可以使用dict.keys()

>>> l1 = list(d.keys())
>>> l1
['firstKey', 'secondKey', 'thirdKey']

But not really needed since list(d) will give you the keys by default.但实际上并不需要,因为list(d)默认会为您提供密钥。

You could assign the two list variables in one line like this:您可以像这样在一行中分配两个列表变量:

d = Counter({'firstKey': 1708, 'secondKey': 1589, 'thirdKey': 1424})

keys,values = map(list,zip(*d.items()))

output: output:

print(keys)
print(values)

['firstKey', 'secondKey', 'thirdKey']
[1708, 1589, 1424]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 以列表为值的字典,使用原始键值的所有可能组合创建字典列表 - Python dict with lists as values, create list of dicts with all possible combinations of original keys-values 如何获取另一个字典中所有值的键的字典 - How to get a dict of the keys for all values in another dict Python 字典创建到自己的列表并在一行中添加具有特定键的所有值 - Python Dict create to own list and add all values with specific Keys in one line python dict.keys() 不返回所有值 - python dict.keys() not returning all values 替换 python 字典中的所有键和操作值并在列表中构造一个新字典 - Replace all keys and manipulate values in a python dict and construct a new dict in a list 将 [dict] 中的所有键转换为 python 中的 dict - Convert all keys inside [dict] to dict in python 在Python中,遍历一个dict并将list或dict的所有值设置为string - In Python, loop through a dict and set all values of list or dict to string PYTHON:获取列表内字典中的所有值 - PYTHON: getting all values in a dict inside a list 将旧 dict 列表与新 dict 列表进行比较,以找出所有列表对象的键和值之间的差异 - Comparing old dict list with new dict list to find the differences across keys and values of all list objects Python:使用带有整数键的 dict() 创建字典? - Python: create dictionary using dict() with integer keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM