我必须在Python中找到列表的平均值.到目前为止这是我的代码
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print reduce(lambda x, y: x + y, l)
Run Code Online (Sandbox Code Playgroud)
我已经得到它所以它将列表中的值加在一起,但我不知道如何将它分成它们?
filter
,map
并且reduce
在Python 2中完美地工作.这是一个例子:
>>> def f(x):
return x % 2 != 0 and x % 3 != 0
>>> filter(f, range(2, 25))
[5, 7, 11, 13, 17, 19, 23]
>>> def cube(x):
return x*x*x
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>> def add(x,y):
return x+y
>>> reduce(add, range(1, 11))
55
Run Code Online (Sandbox Code Playgroud)
但是在Python 3中,我收到以下输出:
>>> filter(f, range(2, 25))
<filter object at 0x0000000002C14908>
>>> map(cube, range(1, 11))
<map object at 0x0000000002C82B70> …
Run Code Online (Sandbox Code Playgroud) 说我想要a.x
为每个元素求和arr
.
arr = [{x:1},{x:2},{x:4}]
arr.reduce(function(a,b){return a.x + b.x})
>> NaN
Run Code Online (Sandbox Code Playgroud)
我有理由相信ax在某些时候是不确定的.
以下工作正常
arr = [1,2,4]
arr.reduce(function(a,b){return a + b})
>> 7
Run Code Online (Sandbox Code Playgroud)
我在第一个例子中做错了什么?
什么时候应该使用reduceLeft
,reduceRight
,foldLeft
,foldRight
,scanLeft
或scanRight
?
我想要一个直觉/概述他们的差异 - 可能有一些简单的例子.
我正在使用Python 3.2.试过这个:
xor = lambda x,y: (x+y)%2
l = reduce(xor, [1,2,3,4])
Run Code Online (Sandbox Code Playgroud)
并得到以下错误:
l = reduce(xor, [1,2,3,4])
NameError: name 'reduce' is not defined
Run Code Online (Sandbox Code Playgroud)
尝试打印reduce
到交互式控制台 - 出现此错误:
NameError: name 'reduce' is not defined
Run Code Online (Sandbox Code Playgroud)
是reduce
在Python 3.2真的删除?如果是这样的话,还有什么选择呢?
有一个很好的Array方法reduce()
可以从Array中获取一个值.例:
[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
Run Code Online (Sandbox Code Playgroud)
实现对象的最佳方法是什么?我想这样做:
{
a: {value:1},
b: {value:2},
c: {value:3}
}.reduce(function(previous, current, index, array){
return previous.value + current.value;
});
Run Code Online (Sandbox Code Playgroud)
但是,Object似乎没有reduce()
实现任何方法.
试图学习F#,但在尝试区分折叠和减少时感到困惑.折叠似乎做同样的事情,但需要额外的参数.是否存在这两种功能存在的合理原因,或者它们是否适合具有不同背景的人?(例如:C#中的字符串和字符串)
以下是从示例中复制的代码段:
let sumAList list =
List.reduce (fun acc elem -> acc + elem) list
let sumAFoldingList list =
List.fold (fun acc elem -> acc + elem) 0 list
printfn "Are these two the same? %A "
(sumAList [2; 4; 10] = sumAFoldingList [2; 4; 10])
Run Code Online (Sandbox Code Playgroud) 在Haskell中实现类似以下内容的最惯用方法是什么:
foldl (+) 0 [1,2,3,4,5]
--> 15
Run Code Online (Sandbox Code Playgroud)
或者它在Ruby中的等价物:
[1,2,3,4,5].inject(0) {|m,x| m + x}
#> 15
Run Code Online (Sandbox Code Playgroud)
显然,Python提供了reduce
函数,这是fold的实现,完全如上所述,然而,有人告诉我,'pythonic'编程方式是避免使用lambda
术语和高阶函数,在可能的情况下更喜欢列表推导.因此,有没有一种首选的方法来折叠Python中的列表或类似列表的结构,而不是reduce
函数,或者是reduce
实现这一目的的惯用方法?
我看到他们在这里被记录在案.它们是一样的吗?为什么Ruby有这么多别名(比如数组的map/collect)?非常感谢.
有没有办法在JavaScript中使用map
/ reduce
/ filter
/ etc a Set
或者我必须自己编写?
这是一些合理的Set.prototype
扩展
Set.prototype.map = function map(f) {
var newSet = new Set();
for (var v of this.values()) newSet.add(f(v));
return newSet;
};
Set.prototype.reduce = function(f,initial) {
var result = initial;
for (var v of this) result = f(result, v);
return result;
};
Set.prototype.filter = function filter(f) {
var newSet = new Set();
for (var v of this) if(f(v)) newSet.add(v);
return newSet;
};
Set.prototype.every = function every(f) {
for (var …
Run Code Online (Sandbox Code Playgroud) reduce ×10
python ×4
fold ×3
javascript ×3
list ×2
alias ×1
arrays ×1
average ×1
ecmascript-6 ×1
f# ×1
filter ×1
inject ×1
lambda ×1
node.js ×1
object ×1
python-3.2 ×1
python-3.x ×1
ruby ×1
scala ×1
set ×1