Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
785 views
in Technique[技术] by (71.8m points)

list - How to get the first value in a python dictionary

I have a dictionary like this:

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?

I want in the end a list:

myList = [1609.32,57813.48,991.31]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this way:

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...