简体   繁体   English

如何从 Python 中的字典中提取所有值?

[英]How can I extract all values from a dictionary in Python?

I have a dictionary d = {1:-0.3246, 2:-0.9185, 3:-3985, ...} .我有一本字典d = {1:-0.3246, 2:-0.9185, 3:-3985, ...}

How do I extract all of the values of d into a list l ?如何将d的所有值提取到列表l中?

If you only need the dictionary keys 1 , 2 , and 3 use: your_dict.keys() .如果您只需要字典键123 ,请使用: your_dict.keys()

If you only need the dictionary values -0.3246 , -0.9185 , and -3985 use: your_dict.values() .如果您只需要字典值-0.3246-0.9185-3985 ,请使用: your_dict.values()

If you want both keys and values use: your_dict.items() which returns a list of tuples [(key1, value1), (key2, value2), ...] .如果你想要键和值都使用: your_dict.items()它返回一个元组列表[(key1, value1), (key2, value2), ...]

Use values()使用values()

>>> d = {1:-0.3246, 2:-0.9185, 3:-3985}

>>> d.values()
<<< [-0.3246, -0.9185, -3985]

For Python 3, you need:对于 Python 3,您需要:

list_of_dict_values = list(dict_name.values())

If you want all of the values, use this:如果你想要所有的值,使用这个:

dict_name_goes_here.values()

If you want all of the keys, use this:如果您想要所有密钥,请使用以下命令:

dict_name_goes_here.keys()

IF you want all of the items (both keys and values), I would use this:如果您想要所有项目(键和值),我会使用这个:

dict_name_goes_here.items()

Call the values() method on the dict.在字典上调用values()方法。

For nested dicts, lists of dicts, and dicts of listed dicts, ... you can use对于嵌套的 dicts、dicts 列表和列出 dicts 的 dicts,...您可以使用

def get_all_values(d):
    if isinstance(d, dict):
        for v in d.values():
            yield from get_all_values(v)
    elif isinstance(d, list):
        for v in d:
            yield from get_all_values(v)
    else:
        yield d 

An example:一个例子:

d = {'a': 1, 'b': {'c': 2, 'd': [3, 4]}, 'e': [{'f': 5}, {'g': 6}]}

list(get_all_values(d)) # returns [1, 2, 3, 4, 5, 6]

PS: I love yield . PS:我喜欢yield ;-) ;-)

If you want all of the values, use this:如果你想要所有的值,使用这个:

dict_name_goes_here.values()

To see the keys:要查看密钥:

for key in d.keys():
    print(key)

To get the values that each key is referencing:要获取每个键引用的值:

for key in d.keys():
    print(d[key])

Add to a list:添加到列表:

for key in d.keys():
    mylist.append(d[key])
d = <dict>
values = d.values()

I know this question been asked years ago but its quite relevant even today.我知道这个问题在几年前就被问过了,但即使在今天它也很重要。

>>> d = {1:-0.3246, 2:-0.9185, 3:-3985}
>>> l = list(d.values())
>>> l
[-0.3246, -0.9185, -3985]

Code of python file containing dictionary包含字典的 python 文件的代码

dict={"Car":"Lamborghini","Mobile":"iPhone"}
print(dict)

If you want to print only values (instead of key) then you can use:如果您只想打印值(而不是键),那么您可以使用:

dict={"Car":"Lamborghini","Mobile":"iPhone"}
for thevalue in dict.values():
    print(thevalue)

This will print only values instead of key from dictionary这将只打印值而不是字典中的键

Bonus: If there is a dictionary in which values are stored in list and if you want to print values only on new line, then you can use:奖励:如果有一个字典,其中值存储在列表中,并且如果您只想在新行上打印值,那么您可以使用:

dict={"Car":["Lamborghini","BMW","Mercedes"],"Mobile":["Iphone","OnePlus","Samsung"]}
nd = [value[i] for value in dict.values()
         for i in range(2)]
print(*nd,sep="\n")

Reference - Narendra Dwivedi - Extract Only Values From Dictionary参考 - Narendra Dwivedi - 仅从字典中提取值

Pythonic duck-typing should in principle determine what an object can do, ie, its properties and methods. Pythonic 的鸭子类型原则上应该确定 object 可以做什么,即它的属性和方法。 By looking at a dictionary object one may try to guess it has at least one of the following: dict.keys() or dict.values() methods.通过查看字典 object,可能会尝试猜测它至少具有以下一种: dict.keys()dict.values()方法。 You should try to use this approach for future work with programming languages whose type checking occurs at runtime, especially those with the duck-typing nature.您应该尝试使用这种方法来处理在运行时进行类型检查的编程语言,尤其是那些具有鸭子类型性质的编程语言。

dictionary_name={key1:value1,key2:value2,key3:value3}
dictionary_name.values()

Normal Dict.values()普通字典.values()

will return something like this会返回这样的东西

dict_values(['value1']) dict_values(['value1'])

dict_values(['value2']) dict_values(['value2'])

If you want only Values use如果您只想使用值

  • Use this用这个

list(Dict.values())[0] # Under the List list(Dict.values())[0] # 列表下

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

相关问题 如何在 Python 的 GUI 中打印字典中的所有值 - How can i print all values from a dictionary in a GUI in Python 如何从值是 Python 中的 2D 列表的字典中有效地提取列中的所有元素? - How do I efficiently extract all elements in a column from a dictionary whose values are 2D lists in Python? 如何提取python字典列表的多个值(即提取一个键的所有值)? - How to extract multiple values of a list of python dictionary (i.e. extract all the values for one key)? 如何从python字典中的列表中提取键的所有实例? - How can I extract all instances of a key from a list within a python dictionary? 如何通过指定值范围从字典中提取数据? - How can I extract data from a dictionary by specifying a range of values? 如何从字典中的元组列表中提取值? - How can I extract values from a list of tuples inside a dictionary? 如何从字典中提取确切的键及其值? - How can I extract exact key and its values from a dictionary? 如何用一系列值替换 Python 字典的所有值? - How can I replace all the values of a Python dictionary with a range of values? Python如何从字典中获取所有值相同的名称键 - Python How can I get all values same name keys from dictionary 如何将元组中的所有值添加到字典中? - How can I add all values from a tuple into a dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM