reduce

NameError: name 'reduce' is not defined in Python

痞子三分冷 提交于 2019-11-26 11:56:16
问题 I\'m using Python 3.2. Tried this: xor = lambda x,y: (x+y)%2 l = reduce(xor, [1,2,3,4]) And got the following error: l = reduce(xor, [1,2,3,4]) NameError: name \'reduce\' is not defined Tried printing reduce into interactive console - got this error: NameError: name \'reduce\' is not defined Is reduce really removed in Python 3.2? If that\'s the case, what\'s the alternative? 回答1: It was moved to functools. 回答2: You can add from functools import reduce before you use the reduce. 回答3: Or if

Why is the final reduce step extremely slow in this MapReduce? (HiveQL, HDFS MapReduce)

橙三吉。 提交于 2019-11-26 11:27:57
问题 Some background information: I\'m working with Dataiku DSS, HDFS, and partitioned datasets. I have a particular job running (Hive query) which has two input datasets - one a very large, partitioned dataset, the other a small (~250 rows, 2 columns), non-partitioned dataset. Let\'s call the partitioned table A, and the non-partitioned table B. Question: The query is of the following format, SELECT a.f1, f2, ..., fn FROM A as a LEFT JOIN B as b ON a.f1 = b.f1 WHERE {PARTITION_FILTER} Here is the

Javascript reduce on array of objects

*爱你&永不变心* 提交于 2019-11-26 10:05:54
问题 Say I want to sum a.x for each element in arr . arr = [{x:1},{x:2},{x:4}] arr.reduce(function(a,b){return a.x + b.x}) >> NaN I have cause to believe that a.x is undefined at some point. The following works fine arr = [1,2,4] arr.reduce(function(a,b){return a + b}) >> 7 What am I doing wrong in the first example? 回答1: After the first iteration your're returning a number and then trying to get property x of it to add to the next object which is undefined and maths involving undefined results in

How to break on reduce method

六月ゝ 毕业季﹏ 提交于 2019-11-26 09:35:46
问题 How can I break the iteration on reduce method? for for (var i = Things.length - 1; i >= 0; i--) { if(Things[i] <= 0){ break; } }; reduce Things.reduce(function(memo, current){ if(current <= 0){ //break ??? //return; <-- this will return undefined to memo, which is not what I want } }, 0) 回答1: UPDATE Some of the commentators make a good point that the original array is being mutated in order to break early inside the .reduce() logic. Therefore, I've modified the answer slightly by adding a

Reduce, fold or scan (Left/Right)?

旧时模样 提交于 2019-11-26 07:51:03
问题 When should I use reduceLeft , reduceRight , foldLeft , foldRight , scanLeft or scanRight ? I want an intuition/overview of their differences - possibly with some simple examples. 回答1: In general, all 6 fold functions apply a binary operator to each element of a collection. The result of each step is passed on to the next step (as input to one of the binary operator's two arguments). This way we can cumulate a result. reduceLeft and reduceRight cumulate a single result. foldLeft and foldRight

When do reduce tasks start in Hadoop?

﹥>﹥吖頭↗ 提交于 2019-11-26 06:56:27
问题 In Hadoop when do reduce tasks start? Do they start after a certain percentage (threshold) of mappers complete? If so, is this threshold fixed? What kind of threshold is typically used? 回答1: The reduce phase has 3 steps: shuffle, sort, reduce. Shuffle is where the data is collected by the reducer from each mapper. This can happen while mappers are generating data since it is only a data transfer. On the other hand, sort and reduce can only start once all the mappers are done. You can tell

Difference between reduce and foldLeft/fold in functional programming (particularly Scala and Scala APIs)?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 03:48:14
问题 Why do Scala and frameworks like Spark and Scalding have both reduce and foldLeft ? So then what\'s the difference between reduce and fold ? 回答1: reduce vs foldLeft A big big difference, not mentioned in any other stackoverflow answer relating to this topic clearly, is that reduce should be given a commutative monoid , i.e. an operation that is both commutative and associative. This means the operation can be parallelized. This distinction is very important for Big Data / MPP / distributed

Finding the average of a list

一曲冷凌霜 提交于 2019-11-26 01:31:46
问题 I have to find the average of a list in Python. This is my code so far l = [15, 18, 2, 36, 12, 78, 5, 6, 9] print reduce(lambda x, y: x + y, l) I\'ve got it so it adds together the values in the list, but I don\'t know how to make it divide into them? 回答1: On Python 3.4+ you can use statistics.mean() l = [15, 18, 2, 36, 12, 78, 5, 6, 9] import statistics statistics.mean(l) # 20.11111111111111 On older versions of Python you can do sum(l) / len(l) On Python 2 you need to convert len to a float

Sum javascript object propertyA values with same object propertyB in array of objects

早过忘川 提交于 2019-11-26 00:29:14
问题 How would one take a javascript array of objects such as: my objArr = [ {key:Mon Sep 23 2013 00:00:00 GMT-0400, val:42}, {key:Mon Sep 24 2013 00:00:00 GMT-0400, val:78}, {key:Mon Sep 25 2013 00:00:00 GMT-0400, val:23}, {key:Mon Sep 23 2013 00:00:00 GMT-0400, val:54}] and merge duplicate keys by summing the values. In order to get something like this: my reducedObjArr = [ {key:Mon Sep 23 2013 00:00:00 GMT-0400, val:96}, {key:Mon Sep 24 2013 00:00:00 GMT-0400, val:78}, {key:Mon Sep 25 2013 00

How to use filter, map, and reduce in Python 3

房东的猫 提交于 2019-11-25 23:33:40
问题 filter , map , and reduce work perfectly in Python 2. Here is an example: >>> 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 But in Python 3, I receive the following outputs: >>> filter(f, range(2, 25)) <filter object at 0x0000000002C14908> >>> map(cube, range(1, 11)) <map object