学习 Python 困难的方法 - 练习 39
Learn Python The Hard Way - Exercise 39
在 Learn Python The Hard Way 的练习 39 中,第 37 到 39 行如下所示:
print "-"*10
for state, abbrev in states.items():
print "%s has the city %s" % (state, abbrev)
我以为我明白了。我认为 Python 是从 "states" 中获取 KEY:VALUE 并将 KEY 分配给 "state" 并将 VALUE 分配给 "abbrev".
但是,当我输入以下代码时,我发现发生了一些奇怪的事情:
print "-"*10
for test in states.items():
print "%s has the city %s" % (test)
它产生与原始代码相同的输出。
但是,它只有在将 %s
放入打印语句两次时才有效。
有人可以解释一下 "test" 发生了什么吗?
"test" 到底是什么?它是元组吗?
它似乎同时包含 KEY
和 states.items()
中的 VALUE
。
我在这里查看了练习 39 的其他一些问题,但没有找到相同的查询。
代码如下(适用于Python 2.7)
# create a mapping of state to abbreviation
states = {
'Oregan': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York' : 'NY',
'Michigan' : 'MI'
}
print "-"*10
for state, abbrev in states.items():
print "%s has the city %s" % (state, abbrev)
print "-"*10
for test in states.items():
print "%s has the city %s" % (test)
states
是一个字典,所以当您调用 for test in states.items()
时,它会将字典的每个项目(一个 tuple
)分配给 test
.
然后您只需像使用 for state, abbrev in states.items():
一样遍历项目并打印它们的键和值
>>> for state in states.items():
print (state) # print all the tuples
('California', 'CA')
('Oregan', 'OR')
('Florida', 'FL')
('Michigan', 'MI')
('New York', 'NY')
所有详细信息都可以在线获得,例如在 Dictionary Iterators 下的 PEP 234 -- Iterators:
Dictionaries implement a tp_iter slot that returns an efficient iterator that iterates over the keys of the dictionary. [...] This means that we can write
for k in dict: ...
which is equivalent to, but much faster than
for k in dict.keys(): ...
as long as the restriction on modifications to the dictionary (either by the loop or by another thread) are not violated.
Add methods to dictionaries that return different kinds of iterators explicitly:
for key in dict.iterkeys(): ...
for value in dict.itervalues(): ...
for key, value in dict.iteritems(): ...
This means that for x in dict
is shorthand for for x in
dict.iterkeys()
.
你的第一个和第二个代码片段之间的 "missing link" 解释了为什么它们是等价的:
print "-"*10
for test in states.items():
state, abbrev = test
print "%s has the city %s" % (state, abbrev)
在 Learn Python The Hard Way 的练习 39 中,第 37 到 39 行如下所示:
print "-"*10
for state, abbrev in states.items():
print "%s has the city %s" % (state, abbrev)
我以为我明白了。我认为 Python 是从 "states" 中获取 KEY:VALUE 并将 KEY 分配给 "state" 并将 VALUE 分配给 "abbrev".
但是,当我输入以下代码时,我发现发生了一些奇怪的事情:
print "-"*10
for test in states.items():
print "%s has the city %s" % (test)
它产生与原始代码相同的输出。
但是,它只有在将 %s
放入打印语句两次时才有效。
有人可以解释一下 "test" 发生了什么吗?
"test" 到底是什么?它是元组吗?
它似乎同时包含 KEY
和 states.items()
中的 VALUE
。
我在这里查看了练习 39 的其他一些问题,但没有找到相同的查询。
代码如下(适用于Python 2.7)
# create a mapping of state to abbreviation
states = {
'Oregan': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York' : 'NY',
'Michigan' : 'MI'
}
print "-"*10
for state, abbrev in states.items():
print "%s has the city %s" % (state, abbrev)
print "-"*10
for test in states.items():
print "%s has the city %s" % (test)
states
是一个字典,所以当您调用 for test in states.items()
时,它会将字典的每个项目(一个 tuple
)分配给 test
.
然后您只需像使用 for state, abbrev in states.items():
>>> for state in states.items():
print (state) # print all the tuples
('California', 'CA')
('Oregan', 'OR')
('Florida', 'FL')
('Michigan', 'MI')
('New York', 'NY')
所有详细信息都可以在线获得,例如在 Dictionary Iterators 下的 PEP 234 -- Iterators:
Dictionaries implement a tp_iter slot that returns an efficient iterator that iterates over the keys of the dictionary. [...] This means that we can write
for k in dict: ...
which is equivalent to, but much faster than
for k in dict.keys(): ...
as long as the restriction on modifications to the dictionary (either by the loop or by another thread) are not violated.
Add methods to dictionaries that return different kinds of iterators explicitly:
for key in dict.iterkeys(): ... for value in dict.itervalues(): ... for key, value in dict.iteritems(): ...
This means that
for x in dict
is shorthand forfor x in dict.iterkeys()
.
你的第一个和第二个代码片段之间的 "missing link" 解释了为什么它们是等价的:
print "-"*10
for test in states.items():
state, abbrev = test
print "%s has the city %s" % (state, abbrev)