9

我需要多次过滤大型列表,但我关心代码的简单性和执行效率。举个例子:

all_things # huge collection of all things

# inefficient but clean code
def get_clothes():
    return filter(lambda t: t.garment, allThings)

def get_hats():
    return filter(lambda t: t.headgear, get_clothes())

我担心我正在迭代衣服清单,而实际上它已经被迭代了。我还想将两个过滤器操作分开,因为它们属于两个不同的类,并且我不想复制 hats 类中的第一个 lambda 函数。

# efficient but duplication of code
def get_clothes():
    return filter(lambda t: t.garment, allThings)

def get_hats():
    return filter(lambda t: t.headgear and t.garment, allThings)

我一直在研究生成器功能,因为它们似乎是要走的路,但我还没有弄清楚如何。

4

3 回答 3

25

首先使用filter/lambda组合将被弃用。当前的函数式编程风格在Python 函数式编程 HOWTO中有描述。

其次,如果你关心效率,而不是构造列表,你应该返回generators。在这种情况下,它们很简单,可以使用生成器表达式

def get_clothes():
    return (t for t in allThings if t.garment)

def get_hats():
    return (t for t in get_clothes() if t.headgear)

或者,如果您愿意,可以使用真正的生成器(据称更 Pythonic):

def get_clothes():
    for t in allThings:
       if t.garment:
           yield t

def get_hats():
    for t in get_clothes():
        if t.headgear:
            yield t

如果由于某种原因,有时您需要list而不是iterator,您可以通过简单的强制转换来构造列表:

hats_list = list(get_hats())

注意,上面不会构造衣服列表,因此效率接近你的重复代码版本。

于 2012-04-27T11:37:40.157 回答
5

我一直在寻找类似的列表过滤,但希望与此处呈现的格式略有不同。

上面的get_hats()调用很好,但重用性有限。我一直在寻找类似的东西get_hats(get_clothes(all_things)),您可以在其中指定源(all_things),然后根据需要指定尽可能少或尽可能多的过滤器get_hats()级别get_clothes()

我找到了一种使用生成器的方法:

def get_clothes(in_list):
    for item in in_list:
        if item.garment:
            yield item

def get_hats(in_list):
    for item in in_list:
        if item.headgear:
            yield item

然后可以通过以下方式调用:

get_hats(get_clothes(all_things))

我测试了原始解决方案、vartec 的解决方案和这个额外的解决方案来查看效率,并且对结果有些惊讶。代码如下:

设置:

class Thing:
    def __init__(self):
        self.garment = False
        self.headgear = False

all_things = [Thing() for i in range(1000000)]

for i, thing in enumerate(all_things):
    if i % 2 == 0:
        thing.garment = True
    if i % 4 == 0:
        thing.headgear = True

原始解决方案:

def get_clothes():
    return filter(lambda t: t.garment, all_things)

def get_hats():
    return filter(lambda t: t.headgear, get_clothes())

def get_clothes2():
    return filter(lambda t: t.garment, all_things)

def get_hats2():
    return filter(lambda t: t.headgear and t.garment, all_things)

我的解决方案:

def get_clothes3(in_list):
    for item in in_list:
        if item.garment:
            yield item

def get_hats3(in_list):
    for item in in_list:
        if item.headgear:
            yield item

vartec的解决方案:

def get_clothes4():
    for t in all_things:
       if t.garment:
           yield t

def get_hats4():
    for t in get_clothes4():
        if t.headgear:
            yield t

计时码:

import timeit

print 'get_hats()'
print timeit.timeit('get_hats()', 'from __main__ import get_hats', number=1000)

print 'get_hats2()'
print timeit.timeit('get_hats2()', 'from __main__ import get_hats2', number=1000)

print '[x for x in get_hats3(get_clothes3(all_things))]'
print timeit.timeit('[x for x in get_hats3(get_clothes3(all_things))]',
                    'from __main__ import get_hats3, get_clothes3, all_things',
                    number=1000)

print '[x for x in get_hats4()]'
print timeit.timeit('[x for x in get_hats4()]',
                    'from __main__ import get_hats4', number=1000)

结果:

get_hats()
379.334653854
get_hats2()
232.768362999
[x for x in get_hats3(get_clothes3(all_things))]
214.376812935
[x for x in get_hats4()]
218.250688076

生成器表达式似乎稍微快一些,我和 vartec 的解决方案之间的时间差异可能只是噪音。但我更喜欢能够以任何顺序应用所需的任何过滤器的灵活性。

于 2012-10-30T03:20:48.083 回答
4

仅一次完成(伪代码):

clothes = list()
hats = list()
for thing in things:
    if thing is a garment:
        clothes.append(thing)
        if thing is a hat:
            hats.append(thing)

一次大通和一小通(列表推导):

clothes = [ x for x in things if x is garment ]
hats = [ x for x in clothes if x is hat ]

如果您想创建整个列表,则使用生成器表达式进行延迟评估是没有意义的,因为您不会变得懒惰。

如果您一次只想处理几件事情,或者如果您的内存受限,请使用@vartec 的生成器解决方案。

于 2012-04-27T11:37:31.107 回答