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
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
用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例程反正擔任代碼..
或更多pythonic,只需使用'key = sum' – wim
@wim:確實; cython也支持這一點。 :-) –
關鍵是沒有問題的,但拉姆達 - 因爲它與其他鍵的作用。 – wim