Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中reduce的语法_Python - Fatal编程技术网

python中reduce的语法

python中reduce的语法,python,Python,我试图从这里模拟reduce的结构: 这是我的代码: def collector(ix, myiter): s = reduce((lambda x,y: x + "," + y), myiter) #syntax error return s 错误是: SyntaxError:无效语法 对于python专业人士来说,这无疑是一个“给我”的答案。代码看起来不错,甚至运行起来也不错。你能用调用堆栈提供完整的异常消息吗?我在这里也试过了,它能工作。它看起来像ipython中的一个bug。它

我试图从这里模拟reduce的结构:

这是我的代码:

def collector(ix, myiter):
  s = reduce((lambda x,y: x + "," + y), myiter) #syntax error
  return s
错误是:

SyntaxError:无效语法


对于python专业人士来说,这无疑是一个“给我”的答案。

代码看起来不错,甚至运行起来也不错。你能用调用堆栈提供完整的异常消息吗?我在这里也试过了,它能工作。它看起来像ipython中的一个bug。它在python中确实可以工作。我现在是一个不快乐的露营者。奇怪。您使用的是Python 2还是Python 3?正如您的链接所提到的,
reduce()
不是Python 3中内置的,您需要从functools导入它。但这不应该给你一个语法错误。我注意到您的函数没有使用
ix
。如果您不考虑,ipython是否也会给出语法错误?例如,
def收集器(myiter):
返回reduce(lambda x,y:x+”,“+y,myiter)
。FWIW,我意识到你正在试验
reduce
,但是你可以用内置的
str.join()
方法做与你的
collector()
相同的事情-
,'.join(myiter)
。通常,IPython会给出一个指向字符的指针,它意识到语法是无效的;你的还原行中的哪个字符是?