久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用生成器和迭代器時 Python 多循環(huán)失敗

Python multi-loop failed when with generator and iterator(使用生成器和迭代器時 Python 多循環(huán)失敗)
本文介紹了使用生成器和迭代器時 Python 多循環(huán)失敗的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要一個 2D 循環(huán),其中第一個循環(huán)使用迭代器,第二個循環(huán)使用生成器,但是這個簡單的函數(shù)無法工作,誰能幫忙檢查一下?

I need a 2D loop of which the first loop uses an iterator and the second uses a generator, but this simple function failed to work, can anyone help to check?

def alphabet(begin, end):
    for number in xrange(ord(begin), ord(end)+1):
        yield chr(number)

def test(a, b):
    for i in a:
        for j in b:
            print i, j

test(xrange(8, 10), alphabet('A', 'C'))

The result shows:
>>> 8 A
>>> 8 B
>>> 8 c

不知道為什么?如果有人可以提供幫助,請?zhí)崆爸轮x.

don't know why? thanks in advance if any one can help.

推薦答案

既然你要求澄清,我就多說一點;但真的 Ignacio 的回答總結得很好:您只能迭代生成器一次.您示例中的代碼嘗試對其進行三次迭代,a 中的每個值一次.

Since you've asked for clarification, I'll say a bit more; but really Ignacio's answer sums it up pretty well: you can only iterate over a generator once. The code in your example tries to iterate over it three times, once for each value in a.

要明白我的意思,請考慮這個簡單的例子:

To see what I mean, consider this simplistic example:

>>> def mygen(x):
...     i = 0
...     while i < x:
...         yield i
...         i += 1
... 
>>> mg = mygen(4)
>>> list(mg)
[0, 1, 2, 3]
>>> list(mg)
[]

mygen 被調用時,它會創(chuàng)建一個可以只迭代一次的對象.當您嘗試再次對其進行迭代時,您會得到一個空的可迭代對象.

When mygen is called, it creates an object which can be iterated over exactly once. When you try to iterate over it again, you get an empty iterable.

這意味著你必須重新調用 mygen每次你想要迭代它`.所以換句話說(使用相當冗長的風格)......

This means you have to call mygen anew, every time you want to iterate over it`. So in other words (using a rather verbose style)...

>>> def make_n_lists(gen, gen_args, n):
...     list_of_lists = []
...     for _ in range(n):
...         list_of_lists.append(list(gen(*gen_args)))
...     return list_of_lists
... 
>>> make_n_lists(mygen, (3,), 3)
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

如果您想將參數(shù)綁定到生成器并將其作為無參數(shù)函數(shù)傳遞,您可以這樣做(使用更簡潔的樣式):

If you wanted to bind your arguments to your generator and pass that as an argumentless function, you could do this (using a more terse style):

>>> def make_n_lists(gen_func, n):
...     return [list(gen_func()) for _ in range(n)]
... 
>>> make_n_lists(lambda: mygen(3), 3)
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

lambda 只是定義了一個匿名函數(shù);以上與此相同:

The lambda just defines an anonymous function; the above is identical to this:

>>> def call_mygen_with_3():
...     return mygen(3)
... 
>>> make_n_lists(call_mygen_with_3, 3)
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

這篇關于使用生成器和迭代器時 Python 多循環(huán)失敗的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現(xiàn)問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 一区二区三区在线 | 欧 | 日本人和亚洲人zjzjhd | 男女性毛片 | 青青激情网 | 九九热国产精品视频 | 在线国产一区二区 | 欧美日韩黄色一级片 | 久久综合九色综合欧美狠狠 | a中文在线视频 | 男女深夜网站 | 久久精品国产一区二区电影 | 欧美久久久久久久久 | 免费黄色av | 国产视频二区 | 日韩欧美综合 | 欧美老少妇一级特黄一片 | 亚洲欧美日韩一区二区 | 成人精品视频 | 97色在线观看免费视频 | 黑人巨大精品欧美一区二区一视频 | 亚洲精品91 | 成人二区 | 成年人网站在线观看视频 | 成人伊人 | 久久久久亚洲 | 成人免费久久 | 成人在线视频免费播放 | 蜜桃视频在线观看免费视频网站www | 欧美精品久久久久久 | 91av在线免费观看 | 国产在线精品免费 | 日本人做爰大片免费观看一老师 | 国产清纯白嫩初高生在线播放视频 | 亚洲精品一区在线观看 | 色久电影| 色综合久久久久 | 亚洲午夜av| 一区二区三区中文字幕 | 色女人天堂| 蜜桃精品视频在线 | 午夜国产 |