How to get the first value in a python dictionary

前端 未结 6 869
遇见更好的自我
遇见更好的自我 2020-12-11 01:02

I have a dictionary like this:

myDict = {  
    \'BigMeadow2_U4\': (1609.32, 22076.38, 3.98),  
    \'MooseRun\': (57813.48, 750187.72, 231.25),  
    \'Hw         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 01:40

    If you want ordered dictionary, use:

    from collections import OrderedDict
    ordered = OrderedDict(
        ('BigMeadow2_U4', (1609.32, 22076.38, 3.98)),  
        ('MooseRun', (57813.48, 750187.72, 231.25)),  
        ('Hwy14_2', (991.31, 21536.80, 6.47)) 
    )
    first_values = [v[0] for v in ordered.values()]
    

    The output order will be exactly as your input order.

提交回复
热议问题