Python:从dict获取值的最简单的方法? [英] Python: simplest way to get list of values from dict?

查看:252
本文介绍了Python:从dict获取值的最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在Java中,将Map作为列表的值为轻松做到 list = map.values(); 。我想知道Python中是否有类似的简单方法来从dict获取值列表。

解决方案

是的在 Python 2 中完全相同的事情:

  d.values()

Python 3 (其中 dict.values 返回一个 视图

 列表(d.values())


What is the simplest way to get a list of the values in a dict in Python?

In Java, getting the values of a Map as a List is as easy as doing list = map.values();. I'm wondering if there is a similarly simple way in Python to get a list of values from a dict.

解决方案

Yes it's the exact same thing in Python 2:

d.values()

In Python 3 (where dict.values returns a view of the dictionary’s values instead):

list(d.values())

这篇关于Python:从dict获取值的最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆