Python学习笔记(十三)迭代和推导式(Iterations and Comprehension)

1.for 循环和可迭代对象是如何关联的?

答:for 循环使用迭代协议(iteration protocol)来遍历它正在迭代的可迭代对象中的项。它首先通过将对象传递到 iter() 来从可迭代对象(iterable)获取迭代器(iterator),并且在 Python 3.X 中每次迭代中调用此迭代器对象的 __next__ 方法,并捕获 StopIteration 异常以确定何时停止循环。方法在 Python 2.X 中被命名为 next 【例如,X.next()】,在 Python 3.X 和 Python 2.X 中都能运行内建函数 next() 。任何支持这种模型的对象在 for 循环和其它迭代上下文【环境/语境】中起作用。对于一些本身就是迭代器的对象,初始 iter 调用无关的,但无害的,没影响。

2.for 循环和列表推导式(list comprehensions)如何关联的?

答:两者都是迭代工具(iteration tools)和迭代上下文【环境/语境】(iteration contexts)。列表推导式是执行普遍的 for 循环任务的一种简洁且通常是有效的方法:收集将表达式应用于可迭代对象中所有项的结果,并表示成为列表的形式。总是可以将列表推导式转换为 for 循环,并且列表推导表达式的一部分在语法上类似于 for 循环的头部。

3.说出 Python 语言四个迭代上下文【环境/语境】(iteration context)。
答:Python中的迭代上下文【环境/语境】包括 for 循环;列表推导式;map 内置函数;in 成员测试表达式;以及内建函数 sorted 、sum 、any 和 all 。这一类还包括内置函数 list 和 tuple、字符串 join 方法和序列赋值(sequence assignments),所有这些方法都使用迭代协议(iteration protocol)(请参阅答案#1)一次迭代一个可迭代对象。


.
4.如今从文本文件逐行读取的最佳方法是什么?
答:如今,从文本文件中读取行的最佳方法是不是显式地读取它:取而代之,在迭代上下文【环境/语境】工具(iteration context tool)(如 for 循环或列表推导式)中打开文件,然后让它读取。通过在每次迭代上运行文件的 next 处理方法一次自动扫描一行。这种方法在编写简单性、内存空间和可能的执行速度需求方面是最好的。

5.你希望看到西班牙宗教法庭使用哪种武器?【这个问题好像与 Monty Python 有关 估计是玩梗】
答:我会接受以下任何一个正确的答案:恐惧(fear)、恐吓(intimidation)、漂亮的红色制服(nice red uniforms)、舒适的椅子(a comfy chair)和柔软的枕头(soft pillows)。

标注:转载《Learning Python 5th Edition》[奥莱理]

1. How are for loops and iterable objects related?
2. How are for loops and list comprehensions related?
3. Name four iteration contexts in the Python language.
4. What is the best way to read line by line from a text file today?
5. What sort of weapons would you expect to see employed by the Spanish Inquisition?

1. The for loop uses the iteration protocol to step through items in the iterable object across which it is iterating. It first fetches an iterator from the iterable by passing the object to iter, and then calls this iterator object’s __next__ method in 3.X on each iteration and catches the StopIteration exception to determine when to stop looping. The method is named next in 2.X, and is run by the next built-in function in both 3.x and 2.X. Any object that supports this model works in a for loop and in all other iteration contexts. For some objects that are their own iterator, the initial iter call is extraneous but harmless.
2. Both are iteration tools and contexts. List comprehensions are a concise and often efficient way to perform a common for loop task: collecting the results of applying an expression to all items in an iterable object. It’s always possible to translate a list comprehension to a for loop, and part of the list comprehension expression looks like the header of a for loop syntactically.
3. Iteration contexts in Python include the for loop; list comprehensions; the map built-in function; the in membership test expression; and the built-in functions sorted, sum, any, and all. This category also includes the list and tuple built-ins, string join methods, and sequence assignments, all of which use the iteration protocol (see answer #1) to step across iterable objects one item at a time.
4. The best way to read lines from a text file today is to not read it explicitly at all: instead, open the file within an iteration context tool such as a for loop or list comprehension, and let the iteration tool automatically scan one line at a time by running the file’s next handler method on each iteration. This approach is generally best in terms of coding simplicity, memory space, and possibly execution speed requirements.
5. I’ll accept any of the following as correct answers: fear, intimidation, nice red uniforms, a comfy chair, and soft pillows.

猜你喜欢

转载自blog.csdn.net/Enderman_xiaohei/article/details/87624652