Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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 2.7 Python3.4和Python2.7中map、enumerate和lambda之间的区别是什么?_Python 2.7_Lambda_Python 3.5_Enumerate - Fatal编程技术网

Python 2.7 Python3.4和Python2.7中map、enumerate和lambda之间的区别是什么?

Python 2.7 Python3.4和Python2.7中map、enumerate和lambda之间的区别是什么?,python-2.7,lambda,python-3.5,enumerate,Python 2.7,Lambda,Python 3.5,Enumerate,我正在做一个项目,我必须计算数据集上dirichlet的分布。我发现了一些由开发的代码 作为一个培训示例,我开发了以下基本代码,以了解并测试给定链接中的代码: from collections import Counter train_list = [1,2,1,2,1,3,4,1,2,3] counter = Counter(train_list) votes = [counter.get(n,0) for n in range(1,5)] prior = [2,2,2,2] posteri

我正在做一个项目,我必须计算数据集上dirichlet的分布。我发现了一些由开发的代码

作为一个培训示例,我开发了以下基本代码,以了解并测试给定链接中的代码:

from collections import Counter

train_list = [1,2,1,2,1,3,4,1,2,3]
counter = Counter(train_list)
votes = [counter.get(n,0) for n in range(1,5)]
prior = [2,2,2,2]
posterior = map(sum, zip(votes, prior))
weights = map(lambda i: (i[0]+1)*i[1], enumerate(posterior))
N = sum(posterior)
print(float(sum(weights)) / N)
我在python 3.5和python 2.7中运行了代码。
在python 3.5中,无论是否更改列列表,我总是得到相同的结果,即等于零。
但在Python2.7中,我得到了一个浮点数,我想这恰好是正确的答案,因为每当我修改train_列表中的值时,它都会发生变化。
我不知道为什么会这样?
原因可能是python 2.7和python 3.5在map、lambda或enumerate函数中的过程不同。
如果有人能帮我修复python 3.5中的代码,我将不胜感激。

我找到了答案。
多亏了我对代码进行了修改,它在python 3.5和python 2.7中都返回了相同的值:

from collections import Counter

train_list = [1,2,1,2,1,3,4,1,2,3]
counter = Counter(train_list)
votes = [counter.get(n,0) for n in range(1,5)]
prior = [2,2,2,2]
posterior = list(map(sum, zip(votes, prior)))
weights = map(lambda i: (i[0]+1)*i[1], enumerate(posterior))
N = sum(posterior)
print(float(sum(weights)) / N)