简体   繁体   中英

dictionary comprehension using list values

I want to use dictionary/list comprehension to create a dictionary multiple lists.

Here are my 3 lists:

city_list = ['new york','boston']
times = ['2021-01-01 19:00:00','2021-01-01 20:00:00']
weather_parameters = ['forecastTimeUtc', 'airtemperature', 'condition']

I want to create a dictionary that looks like that:

{'new york':[{'forecastTimeUtc': '2021-01-01 20:00:00', 'airTemperature': 0, 'conditionCode': 0}, {'forecastTimeUtc': '2021-01-01 21:00:00', 'airTemperature': 0, 'conditionCode': 0}], 

'boston': [{'forecastTimeUtc': '2021-01-01 20:00:00', 'airTemperature': 0, 'conditionCode': 0}, {'forecastTimeUtc': '2021-01-01 21:00:00', 'airTemperature': 0, 'conditionCode': 0}]}

The challenge is to multiply the list that comes after the city based on the how many values are inside of 'forecastTimeUtc'.

The other values for keys 'airTemperature' and 'conditionCode' should stay 0 by default.

In general, we use list comprehensions when they can be concise and make your code elegant. I am afraid that wanting a single dictionary comprehension will confuse the readers of your code and I would suggest building multiple nested for loops.

Nonetheless, here is a dictionary compression that meets your needs:

{city:[{weather_parameter: (time if weather_parameter == 'forecastTimeUtc' else 0) for weather_parameter in weather_parameters} for time in times] for city in city_list}

Step-by-step process

Without any list comprehension, the code is pretty heavy

weather_per_city = {}
for city in city_list:
    params_list = []
    for time in times:
        param_dict = {}
        for weather_parameter in weather_parameters:
            if weather_parameter == 'forecastTimeUtc':
                param_dict[weather_parameter] = time
            else:    
                param_dict[weather_parameter] = 0
        params_list.append(param_dict)
    weather_per_city[city] = params_list

Still no list comprehension, but with a ternary operator to start simplifying

weather_per_city = {}
for city in city_list:
    params_list = []
    for time in times:
        param_dict = {}
        for weather_parameter in weather_parameters:
            param_dict[weather_parameter] = time if weather_parameter == 'forecastTimeUtc' else 0
        params_list.append(param_dict)
    weather_per_city[city] = params_list

First dictionary comprehension:

weather_per_city = {}
for city in city_list:
    params_list = []
    for time in times:
        param_dict = {weather_parameter:(time if weather_parameter == 'forecastTimeUtc' else 0) for weather_parameter in weather_parameters}
        params_list.append(param_dict)
    weather_per_city[city] = params_list

And with a second comprehension (dictionary + list):

weather_per_city = {}
for city in city_list:
    params_list = [{weather_parameter:(time if weather_parameter == 'forecastTimeUtc' else 0) for weather_parameter in weather_parameters} for time in times]
    weather_per_city[city] = params_list

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