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

我有字典d = {1:-0.3246, 2:-0.9185, 3:-3985, ...}

如何将的所有值提取d到列表中l

I have a dictionary d = {1:-0.3246, 2:-0.9185, 3:-3985, ...}.

How do I extract all of the values of d into a list l?


回答 0

如果你只需要字典的键123使用:your_dict.keys()

如果你只需要在字典中的值-0.3246-0.9185-3985使用:your_dict.values()

如果您想同时使用键和值,请使用:your_dict.items()返回一个元组列表[(key1, value1), (key2, value2), ...]

If you only need the dictionary keys 1, 2, and 3 use: your_dict.keys().

If you only need the dictionary values -0.3246, -0.9185, and -3985 use: your_dict.values().

If you want both keys and values use: your_dict.items() which returns a list of tuples [(key1, value1), (key2, value2), ...].


回答 1

使用 values()

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

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

Use values()

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

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

回答 2

如果需要所有值,请使用以下命令:

dict_name_goes_here.values()

如果需要所有键,请使用以下命令:

dict_name_goes_here.keys()

如果您想要所有项目(键和值),则可以使用以下命令:

dict_name_goes_here.items()

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()

回答 3

values()在dict上调用方法。

Call the values() method on the dict.


回答 4

对于Python 3,您需要:

list_of_dict_values = list(dict_name.values())

For Python 3, you need:

list_of_dict_values = list(dict_name.values())

回答 5

对于嵌套字典,字典列表和列出字典的字典,…您可以使用

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 

一个例子:

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:我爱yield。;-)

For nested dicts, lists of dicts, and dicts of listed dicts, … you can use

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. ;-)


回答 6

如果需要所有值,请使用以下命令:

dict_name_goes_here.values()

If you want all of the values, use this:

dict_name_goes_here.values()

回答 7

d = <dict>
values = d.values()
d = <dict>
values = d.values()

回答 8

要查看按键:

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

要获取每个键所引用的值:

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

添加到列表:

for key in d.keys():
    mylist.append(d[key])

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])

回答 9

Python的鸭式输入原则上应确定对象可以执行的操作,即其属性和方法。通过查看字典对象,可以尝试猜测它是否具有以下至少一项:dict.keys()dict.values()方法。您应该尝试将这种方法用于将来在运行时会进行类型检查的编程语言,尤其是具有鸭式语言的语言。

Pythonic duck-typing should in principle determine what an object can do, i.e., its properties and methods. 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. 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.


回答 10

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。