timezone_countries = {timezone: country
for country, timezones in country_timezones.iteritems()
for timezone in timezones}
这就是它的作用:
>>> from pytz import country_timezones
>>> timezone_countries = {timezone: country
for country, timezones in country_timezones.iteritems()
for timezone in timezones}
>>> timezone_countries['Europe/Zurich']
u'CH'
from pytz import country_timezones
timezone_country = {}
for countrycode in country_timezones:
timezones = country_timezones[countrycode]
for timezone in timezones:
timezone_country[timezone] = countrycode
这很容易。您有一个
dict
将每个国家映射到一个时区列表。您希望将每个list
成员映射回dict
。与其只给出答案,不如看看如何得到答案。
首先,如果您刚刚有一个
dict
将每个国家映射到一个时区,这将是一个简单的反向映射:但这不起作用;你有一个时区列表的映射,你希望该列表中的每个时区都映射回该国。英文描述“列表中的每个时区”可以轻松地翻译为Python:
这就是它的作用:
旁注:你没有提到Python2和3,所以我假设是2。如果在3上,将
iteritems
更改为items
,输出将是'CH'
,而不是u'CH'
。您可以使用
pytz
中的country_timezones
对象并生成一个逆映射:现在只需使用生成的字典:
相关问题 更多 >
编程相关推荐