Python lambda不接受元组参数 [英] Python lambda does not accept tuple argument
问题描述
我正在两台PC上运行带有PyDev v2.6插件的Eclipse SDK v3.6,带有Linux和Windows。
I am running Eclipse SDK v3.6 with PyDev v2.6 plugin on two PC, with Linux and Windows.
我想通过一个元组作为参数,如(例如):
I would like to pass a tuple as an argument, like (just example):
foo = lambda (x,y): (y,x)
print (foo((1,2)))
这个工作在Linux上,并给出结果: / p>
This works on Linux and gives the result:
(2,1)
(2,1)
在Windows上它会引发错误:
On Windows it rises an error:
foo = lambda (x,y): (y,x)
^
SyntaxError: invalid syntax
如何解决问题?
提前感谢...
How to solve the problem? Thanks in advance...
感谢 interjay 及其对 PEP 3113 我发现一个元组参数从3.0版本的Python中弃用。
Thanks to interjay and his reference to PEP 3113 I found that a tuple parameter is deprecated from 3.0 version of Python.
所以,没有工作,看起来像
So, the code which did not worked and looked as
accepts = filter(lambda q1, q2: (q1, q2) in states, [(q1, q2) for q1 in D1.accepts for q2 in D2.accepts])
现在适用于版本2.7和版本3.0的Python并且看起来像
now works on both version 2.7 and version 3.0 of Python and looks as
accepts = filter(lambda q: (q1, q2) in states, [(q1, q2) for q1 in D1.accepts for q2 in D2.accepts])
其中接受
推荐答案
您可能在Windows上运行Python 3.x,在Linux上运行Python 2.x 。在Python 3中删除了解包元组参数的功能:请参阅 PEP 3113 。
You are probably running Python 3.x on Windows, and Python 2.x on Linux. The ability to unpack tuple parameters was removed in Python 3: See PEP 3113.
您可以手动解压缩元组,这对Python 2.x和3.x都有效:
You can manually unpack the tuple instead, which would work on both Python 2.x and 3.x:
foo = lambda xy: (xy[1],xy[0])
或者:
def foo(xy):
x,y = xy
return (y,x)
这篇关于Python lambda不接受元组参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!