艰难地学习Python-练习39 [英] Learn Python The Hard Way - Exercise 39

查看:82
本文介绍了艰难地学习Python-练习39的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习Python困难之路"练习39中,第37至39行如下所示:

print "-"*10
for state, abbrev in states.items():
    print "%s has the city %s" % (state, abbrev)

我以为我明白这一点.我以为Python是从状态"中获取KEY:VALUE并将KEY分配给"state",而将VALUE分配给"abbrev".

但是,当我输入以下代码时,我发现了一件奇怪的事情:

print "-"*10
for test in states.items():
    print "%s has the city %s" % (test)

它产生与原始代码相同的输出. 但是,只有将%s放入打印语句两次后,它才能起作用.

有人可以解释测试"发生了什么吗? 测试"到底是什么?是元组吗? 它似乎同时包含states.items()中的KEYVALUE.

我在这里查看了练习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()时,它将字典的每个项目(a tuple)分配给test. /p>

然后,您就像在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')

所有详细信息均可在线获得,例如在 PEP 234-迭代器字典迭代器下的a>:

  • 字典实现了一个tp_iter插槽,该插槽返回一个有效的迭代器,该迭代器对字典的键进行迭代. [...]这意味着我们可以写

    for k in dict: ... 
    

    等同于,但比

    快得多

    for k in dict.keys(): ... 
    

    只要不违反对字典进行修改的限制(无论是通过循环还是通过另一个线程).

  • 将方法添加到字典中,以显式返回不同类型的迭代器:

    for key in dict.iterkeys(): ...
    
    for value in dict.itervalues(): ...
    
    for key, value in dict.iteritems(): ...
    

    这意味着for x in dictfor x in dict.iterkeys()的简写.

On Exercise 39 of Learn Python The Hard Way, lines 37 to 39 look like this:

print "-"*10
for state, abbrev in states.items():
    print "%s has the city %s" % (state, abbrev)

I thought I understood this. I thought Python was taking the KEY:VALUE from "states" and assigning the KEY to "state" and the VALUE to "abbrev".

However, I found something strange happened when I entered the following code:

print "-"*10
for test in states.items():
    print "%s has the city %s" % (test)

It produces the same output as the original code. But, it only works if you put the %s into the print statement twice.

Can someone explain what is happening with "test"? What exactly is "test"? Is it a Tuple? It seems to contain both the KEY and the VALUE from states.items().

I have looked through some of the other questions on Exercise 39 here and I haven't found the same query.

The code is listed below (for 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 is a dictionary, so when you called for test in states.items() it assigns each item of the dictionary (a tuple) to test.

Then you are just iterating over the items and printing their keys and values as you would with 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')

All the details are available online, for instance in PEP 234 -- Iterators under Dictionary 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().

这篇关于艰难地学习Python-练习39的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆