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
247 views
in Technique[技术] by (71.8m points)

python - 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.

question from:https://stackoverflow.com/questions/66060373/dictionary-comprehension-using-list-values

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

1 Reply

0 votes
by (71.8m points)

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

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

...