【发布时间】:2020-04-27 12:00:59
【问题描述】:
我有这本词典,
states = {
'CT': 'Connecticut',
'CA': 'California',
'NY': 'New York',
'NJ': 'New Jersey'
}
和代码在这里..
state2 = {state: abbrev for abbrev, state in states.items()}
我试图了解abbrev for abbrev 的工作原理和方式。我也不清楚state: 到底是什么。我得到第二部分(states.items() 中的状态)。这个的输出给出了
{'Connecticut': 'CT', 'California': 'CA', 'New York': 'NY', 'New Jersey': 'NJ'}
但我不确定这是如何工作的。提前谢谢你。
【问题讨论】:
-
这能回答你的问题吗? Create a dictionary with list comprehension
标签: python dictionary dictionary-comprehension iterable-unpacking