2014-02-10 24 views
0
def test(): 
    return sorted([(a,b) for a in xrange(10) for b in xrange(10)], 
     key=lambda (x,y): x + y) 

以上是有效的python代码,但在cython中触发错误。错误消息是Expected ')', found ','cython不支持用键排序吗?

这里有什么问题?

的Python 2.7,用Cython 0.19.2

+0

关键是没有问题的,但拉姆达 - 因为它与其他键的作用。 – wim

回答

4

用Cython不支持nested tuple argument unpacking

lambda使用嵌套元组参数:

lambda (x,y): x + y 

替换有:

lambda x: x[0] + x[1] 

甚至只是:

sum 

,也许在某些itertools.product()拌过这里,为在:

from itertools import product 

def test(): 
    return sorted(product(xrange(10), repeat=2), key=sum) 

但你最终都与主要由C例程反正担任代码..

+4

或更多pythonic,只需使用'key = sum' – wim

+0

@wim:确实; cython也支持这一点。 :-) –