如何在python字典中获取第一个值 [英] How to get the first value in a python dictionary

查看:3629
本文介绍了如何在python字典中获取第一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的字典:

myDict = {  
    'BigMeadow2_U4': (1609.32, 22076.38, 3.98),  
    'MooseRun': (57813.48, 750187.72, 231.25),  
    'Hwy14_2': (991.31, 21536.80, 6.47)  
}

如何获取字典中每个项目的第一个值?

How can I get the first value of each item in my dicitionary?

我最后想要一个列表:

myList = [1609.32,57813.48,991.31]

推荐答案

尝试这种方式:

my_list = [elem[0] for elem in your_dict.values()]

桌面: 我认为您不应该使用camelcase,这不是python方式

Offtop: I think you shouldn't use camelcase, it isn't python way

UPD: inspectorG4dget注意,结果将不同.这是正确的.您应该使用collections.OrderedDict正确实现此目的.

UPD: inspectorG4dget notes, that result won't be same. It's right. You should use collections.OrderedDict to implement this correctly.

from collections import OrderedDict
my_dict = OrderedDict({'BigMeadow2_U4': (1609.32, 22076.38, 3.98), 'MooseRun': (57813.48, 750187.72, 231.25), 'Hwy14_2': (991.31, 21536.80, 6.47) })

这篇关于如何在python字典中获取第一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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