懒惰的变量评估

时间:2016-08-23 01:41:04

标签: python comparison lexicographic

我想按字典顺序比较两个列表,但是应该在需要时计算列表中的值。例如,对于这两个列表

a = list([1, 3, 3])
b = list([1, 2, 2])

(a < b) == False
(b < a) == True

我希望列表中的值是函数,在ab的情况下,index = 2的值(即函数)不会被计算为index = 1(a[1]==3, b[1]==2)的值已足以确定b < a

一种选择是手动比较元素,这可能就是当我找不到允许我使用列表比较器的解决方案时我会做什么,但我发现手动循环比列表的内置比较器慢一点,这就是我想要使用它的原因。

更新

这是一种完成我想要做的事情的方法,但我想知道是否有任何内置函数可以更快地完成这项工作(并且利用列表的这一功能)。

def lex_comp(a, b):
  for func_a, func_b in izip(a, b):
    v_a = func_a()
    v_b = func_b()
    if v_a < v_b: return -1
    if v_b > v_a: return +1
  return 0


def foo1(): return 1
def foo2(): return 1

def bar1(): return 1
def bar2(): return 2

def func1(): return ...
def func2(): return ...

list_a = [foo1, bar1, func1, ...]
list_b = [foo2, bar2, func2, ...]

# now you can use the comparator for instance to sort a list of these lists
sort([list_a, list_b], cmp=lex_comp)

3 个答案:

答案 0 :(得分:2)

试试这个(函数的额外参数仅用于说明目的):

import itertools

def f(a, x):
    print "lazy eval of {}".format(a)
    return x

a = [lambda: f('a', 1), lambda: f('b', 3), lambda: f('c', 3)]
b = [lambda: f('d', 1), lambda: f('e', 2), lambda: f('f', 2)]
c = [lambda: f('g', 1), lambda: f('h', 2), lambda: f('i', 2)]

def lazyCmpList(a, b):
    l = len(list(itertools.takewhile(lambda (x, y): x() == y(), itertools.izip(a, b))))
    if l == len(a):
        return 0
    else:
        return cmp(a[l](), b[l]())

print lazyCmpList(a, b)
print lazyCmpList(b, a)
print lazyCmpList(b, c)

产地:

lazy eval of a
lazy eval of d
lazy eval of b
lazy eval of e
-1
lazy eval of d
lazy eval of a
lazy eval of e
lazy eval of b
1
lazy eval of d
lazy eval of g
lazy eval of e
lazy eval of h
lazy eval of f
lazy eval of i
0

请注意,代码假定函数列表的长度相同。它可以被增强以支持不相等的列表长度,你必须定义逻辑是什么,即cmp([f1, f2, f3], [f1, f2, f3, f1])应该产生什么?

我还没有比较速度但是考虑到你更新的代码我会想象任何加速都是边缘的(在C代码而不是Python中循环完成)。这个解决方案实际上可能更慢,因为它更复杂并且涉及更多的内存分配。

鉴于您正在尝试通过评估它们来对函数列表进行排序,因此将评估函数,即O(nlogn)次,因此您的最佳加速可能是使用memoization来避免重复重估的功能。

答案 1 :(得分:1)

这是一种使用延迟评估的方法:

>>> def f(x):
...   return 2**x
... 
>>> def g(x):
...   return x*2
... 
>>> [f(x) for x in range(1,10)]
[2, 4, 8, 16, 32, 64, 128, 256, 512]
>>> [g(x) for x in range(1,10)]
[2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> zipped = zip((f(i) for i in range(1,10)),(g(i) for i in range(1,10)))
>>> x,y = next(itertools.dropwhile(lambda t: t[0]==t[1],zipped))
>>> x > y
True
>>> x < y
False
>>> x
8
>>> y
6
>>> 

答案 2 :(得分:1)

我做了一些测试,发现@juanpa的答案和我更新中的版本是最快的版本:

import random
import itertools
import functools

num_rows = 100
data = [[random.randint(0, 2) for i in xrange(10)] for j in xrange(num_rows)]

# turn data values into functions.
def return_func(value):
    return value

list_funcs = [[functools.partial(return_func, v) for v in row] for row in data]


def lazy_cmp_FujiApple(a, b):
    l = len(list(itertools.takewhile(lambda (x, y): x() == y(), itertools.izip(a, b))))
    if l == len(a):
        return 0
    else:
        return cmp(a[l](), b[l]())

sorted1 = sorted(list_funcs, lazy_cmp_FujiApple)
%timeit sorted(list_funcs, lazy_cmp_FujiApple)
# 100 loops, best of 3: 2.77 ms per loop

def lex_comp_mine(a, b):
    for func_a, func_b in itertools.izip(a, b):
        v_a = func_a()
        v_b = func_b()
        if v_a < v_b: return -1
        if v_a > v_b: return +1
    return 0

sorted2 = sorted(list_funcs, cmp=lex_comp_mine)
%timeit sorted(list_funcs, cmp=lex_comp_mine)
# 1000 loops, best of 3: 930 µs per loop

def lazy_comp_juanpa(a, b):
    x, y = next(itertools.dropwhile(lambda t: t[0]==t[1], itertools.izip(a, b)))
    return cmp(x, y)

sorted3 = sorted(list_funcs, cmp=lazy_comp_juanpa)
%timeit sorted(list_funcs, cmp=lex_comp_mine)
# 1000 loops, best of 3: 949 µs per loop

%timeit sorted(data)
# 10000 loops, best of 3: 45.4 µs per loop

# print sorted(data)
# print [[c() for c in row] for row in sorted1]
# print [[c() for c in row] for row in sorted2]
# print sorted3

我想创建一个中间列表会损害@ FujiApple版本的性能。在原始data列表上运行比较器版本并将运行时与Python的本机列表排序进行比较时,我注意到我的版本慢了大约10倍(501μs对每个循环45.4μs)。我想这是不容易接近Python本机实现的性能......