空列表与空发生器 [英] empty lists vs empty generators

查看:76
本文介绍了空列表与空发生器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用生成器和迭代器越来越多地传递列表,并且更喜欢它们。但是,我不清楚

检测空发生器的最佳方法(一个不会返回任何物品)

当需要进行某种特殊情况处理时。


处理空列表的典型代码:

if somelist:

for x in somelist:

something(x)

else:

empty_list_special_case


但这不适用于迭代器 - a生成器是true

,无论它是否会返回任何项目。 (我明白了

为什么)。


我所知道的最接近的等价物是:

n = 0

表示n,x表示枚举(somegenerator()):

something(x)

如果n == 0:

empty_list_special_case


这看起来很尴尬 - 对我来说并不容易,而且

引入了另一个变量。


Q1:有没有更好的或替代的方法来处理这个问题?

Q2:有没有办法处理列表和生成器,所以我没有
我不得不担心我有哪一个?


谢谢,

Brian。

I''m using using generators and iterators more and more intead of
passing lists around, and prefer them. However, I''m not clear on the
best way to detect an empty generator (one that will return no items)
when some sort of special case handling is required.

Typical code for handling an empty list:
if somelist:
for x in somelist:
something(x)
else:
empty_list_special_case

But this doesn''t work with iterators -- a generator is "true"
regardless of whether its going to return any items. (I understand
why).

The closest equivalent I know of is:
n = 0
for n, x in enumerate(somegenerator()):
something(x)
if n == 0:
empty_list_special_case

Which seems rather awkward -- doesn''t read as easily for me, and
introduces another variable.

Q1: Is there a better or alternate way to handle this?
Q2: Is there a way that handles both lists and generators, so I don''t
have to worry about which one I''ve got?

Thanks,
Brian.

推荐答案

Brian Roberts写道:
Brian Roberts wrote:
我正在使用生成器和迭代器越来越多地传递列表,并且更喜欢它们。但是,我不清楚检测空发电机的最佳方法(不会返回任何物品)
需要进行某种特殊情况处理时。

通常情况下,生成器的工作是发出类似

这样的信号。我想可能的方法可能是:


类GeneratorEmpty:传递


def generator():

if不是X:

提高GeneratorEmpty
我在X中的


收益我


尝试:

for x in generator

something(x)

除了GeneratorEmpty:

generator_special_case


诀窍是当生成器引发异常时它们会终止。

虽然这可能不是你想要的。问题是你

无法知道发电机是否会返回任何元素,直到你调用

next()方法。


Q2:有没有办法处理列表和生成器,所以我不必担心我有哪一个?
I''m using using generators and iterators more and more intead of
passing lists around, and prefer them. However, I''m not clear on the
best way to detect an empty generator (one that will return no items)
when some sort of special case handling is required.

Usually it will be the job of the generator to signal something like
this. I think a possible way might be:

class GeneratorEmpty: pass

def generator():
if not X:
raise GeneratorEmpty
for i in X:
yield i

try:
for x in generator
something (x)
except GeneratorEmpty:
generator_special_case

The trick is that when generators raise exceptions they terminate.
Although this is probably not what you want. The thing is that you
cannot know if a generator will return any elements until you call
its next() method.

Q2: Is there a way that handles both lists and generators, so I don''t
have to worry about which one I''ve got?




我不认为这是可能的。必须调用一个生成器(

next()),以便其代码接管并查看它是否为空或

不是。与清单不同。

jfj



I don''t think this is possible. A generator must be called (with
next()) in order for its code to take over and see if it is empty or
not. Unlike the list.
jfj


文章< 50 ************* ************@posting.google.com> ,
br***@mirror.org (Brian Roberts)写道:
In article <50*************************@posting.google.com> ,
br***@mirror.org (Brian Roberts) wrote:
我正在使用生成器和迭代器越来越多地传递列表,并且更喜欢它们。但是,我不清楚检测空发生器(一种不会返回任何物品)的最佳方法
当需要进行某种特殊情况处理时。
I''m using using generators and iterators more and more intead of
passing lists around, and prefer them. However, I''m not clear on the
best way to detect an empty generator (one that will return no items)
when some sort of special case handling is required.



我能想出的最好的事情就是依赖于foo中物品的价格:

传递<如果foo产生任何项目,
只定义项目。假设在执行for循环之前没有定义item

,你可以检查它是否在循环之后定义了它,并用它来判断foo是否为foo是一个空列表或生成器。

这是一个演示。不幸的是,我不确定它是否真的比你的方式更清洁(但至少它没有添加任何无关的变量)

#创建一个产生n个项目的迭代器。

class gen:

def __init __(self,n):

self.n = n


def __iter __(自我):

我在范围内(self.n):

收益无


def checkEmpty(genOrList):

genOrList中的项目:

pass


尝试:

item

print"%s have items" %genOrList
除了NameError之外的


print"%s is empty" %genOrList


checkEmpty(gen(0))

checkEmpty(gen(1))

checkEmpty([])< br $> b $ b checkEmpty([1])


--------------


Roy-Smiths-Computer:play



The best I can come up with is to depend on the fact that

for item in foo:
pass

only defines item if foo yields any items. Assuming item is not defined
before you execute the for loop, you can check to see if it''s defined after
the loop, and use that to tell if foo was an empty list or generator.
Here''s a demo. Unfortunately, I''m not sure if it''s really any cleaner than
your way (but at least it doesn''t add any extraneous variables)
# Creates an iterator which yields n items.
class gen:
def __init__(self, n):
self.n = n

def __iter__(self):
for i in range(self.n):
yield None

def checkEmpty(genOrList):
for item in genOrList:
pass

try:
item
print "%s had items" % genOrList
except NameError:
print "%s was empty" % genOrList

checkEmpty(gen(0))
checkEmpty(gen(1))
checkEmpty([])
checkEmpty([1])

--------------

Roy-Smiths-Computer:play


./ gen.py

< __ main __。gen instance at 0x36c620>是空的

< __ main __。生成实例在0x36c620>有物品

[]是空的

[1]有物品
./gen.py
<__main__.gen instance at 0x36c620> was empty
<__main__.gen instance at 0x36c620> had items
[] was empty
[1] had items


这篇关于空列表与空发生器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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