2014-02-10 23 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也支持這一點。 :-) –