简体   繁体   中英

Putting a list of integers into an existing dictionary

This is the Bonus part of the "Mountain Heights 3" exercise from http://introtopython.org/dictionaries.html .

I have this dictionary which shows 5 mountains and their heights in meters.

mountains_meters = {'Mount Everest' : 8848,
                    'K2' : 8611,
                    'Kangchenjunga' : 8586,
                    'Lhotse' : 8516,
                    'Makalu' : 8485,
                    }

and the question asks to define a function that reads through the height in meters and returns a list of the height in feet, given the conversion 1 meter = 3.28 feet.

feet = []
def meters_to_feet(dictionary):
    for value in dictionary.values(): 
        feet.append(round(value * 3.28))

The question then asks to create a nested dictionary with the structure {'everest': [8848, 29021]}

I'm unsure how to get my list of heights in feet into the existing mountains_meters dictionary.

[29021, 28244, 28162, 27932, 27831] into

mountains_meters = {'Mount Everest' : 8848,
                    'K2' : 8611,
                    'Kangchenjunga' : 8586,
                    'Lhotse' : 8516,
                    'Makalu' : 8485,
                    }

Why don't you do it in a single loop?

for k,v in mountains_meters.items():
    mountains_meters[k] = [v, round(v*3.28)]
    feet.append(round(v * 3.28))

Approach - 1

You can use a tuple or list as a value for each respective mountain range

Each index will define the unit of measurement

from pprint import pprint

mountains_meters = {'Mount Everest' : 8848,
                    'K2' : 8611,
                    'Kangchenjunga' : 8586,
                    'Lhotse' : 8516,
                    'Makalu' : 8485,
                    }

for key in mountains_meters:
    meters = mountains_meters[key]
    feet = round(meters* 3.28)
    mountains_meters[key] = (meters,feet)

>>> pprint(mountains_meters)
{'K2': (8611, 28244),
 'Kangchenjunga': (8586, 28162),
 'Lhotse': (8516, 27932),
 'Makalu': (8485, 27831),
 'Mount Everest': (8848, 29021)}

Approach - 2

You can create a secondary Dictionary to hold the feet conversion

from pprint import pprint

mountains_meters = {'Mount Everest' : 8848,
                    'K2' : 8611,
                    'Kangchenjunga' : 8586,
                    'Lhotse' : 8516,
                    'Makalu' : 8485,
                    }

mountains_feets = {}

for key in mountains_meters:
    meters = mountains_meters[key]
    feet = round(meters* 3.28)
    mountains_feets [key] = feet

>>> pprint(mountains_feets)
{'K2': 28244,
 'Kangchenjunga': 28162,
 'Lhotse': 27932,
 'Makalu': 27831,
 'Mount Everest': 29021}

You can do it like that:

def m_to_feet(m):
    return round(3.28 * m)

nested_dict = {}
for mountain, height_m in mountains_meters.items():
    nested_dict[mountain] = [height_m, m_to_feet(height_m)]

Or in a short oneliner with a dict comprehension:

def m_to_feet(m):
    return round(3.28 * m)

nested_dict = {k: [v, m_to_feet(v)] for k, v in mountains_meters.items()}
def h_f(mountains):
    for key, value in mountains.items():
        mountains[key] = [value, round(value*3.28)]
    return mountains


mountains_meters = {'Mount Everest': 8848,
                    'K2': 8611,
                    'Kangchenjunga': 8586,
                    'Lhotse': 8516,
                    'Makalu': 8485,
                    }
print(h_f(mountains_meters))

Output

{'Mount Everest': [8848, 29021], 'K2': [8611, 28244], 'Kangchenjunga': [8586, 28162], 'Lhotse': [8516, 27932], 'Makalu': [8485, 27831]}
for i, k in enumerate(mountains_meters):
     mountains_meters[k] = [mountains_meters[k], feet[i]]
 
>>> mountains_meters
{'Mount Everest': [8848, 29021], 'K2': [8611, 28244], 'Kangchenjunga': [8586, 28162], 'Lhotse': [8516, 27932], 'Makalu': [8485, 27831]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM