reduce

Reduce by key in python

谁说我不能喝 提交于 2019-12-08 17:23:14
问题 I'm trying to think through the most efficient way to do this in python. Suppose I have a list of tuples: [('dog',12,2), ('cat',15,1), ('dog',11,1), ('cat',15,2), ('dog',10,3), ('cat',16,3)] And suppose I have a function which takes two of these tuples and combines them: def my_reduce(obj1, obj2): return (obj1[0],max(obj1[1],obj2[1]),min(obj1[2],obj2[2])) How do I perform an efficient reduce by 'key' where the key here could be the first value, so the final result would be something like: [(

Merge n amount of objects from array into one array based on id [duplicate]

醉酒当歌 提交于 2019-12-08 15:23:04
问题 This question already has answers here : Merge n object from array into one array based on id (4 answers) Closed 16 days ago . I'm trying to merge n objects from an array of objects listed below. I tried to use reduce method, but I can't understand what I'm doing wrong, still new to advance js methods. const array = [ { data: { '1': { foo: 'bar', test: true }, '4': { foo: 'boor' } } }, { data: { '1': { x: 'o', test2: false } } } ]; const result = Object.values( array.reduce((r, { data }) => {

Spark Python - how to use reduce by key to get minmum/maximum values

筅森魡賤 提交于 2019-12-08 01:38:50
问题 I have a sample data of maximum and minimum temperatures of some cities in csv format. Mumbai,19,30 Delhi,5,41 Kolkata,20,40 Mumbai,18,35 Delhi,4,42 Delhi,10,44 Kolkata,19,39 I want to find out all time lowest temperature recorded for each city using a spark script in Python. Here is my script cityTemp = sc.textFile("weather.txt").map(lambda x: x.split(',')) # convert it to pair RDD for performing reduce by Key cityTemp = cityTemp.map(lambda x: (x[0], tuple(x[1:]))) cityTempMin = cityTemp

Native implementation of reduceRight in JavaScript is wrong

半世苍凉 提交于 2019-12-07 21:48:54
问题 For an associative operation f over the elements of array a , the following relation should hold true: a.reduce(f) should be equivalent to a.reduceRight(f) . Indeed, it does hold true for operations that are both associative and commutative. For example: var a = [1,2,3,4,5,6,7,8,9,0]; alert(a.reduce(add) === a.reduceRight(add)); function add(a, b) { return a + b; } However it doesn't hold true for operations that are associative but not commutative. For example: var a = [[1,2],[3,4],[5,6],[7

Inversion sum is not correct using Stream.reduce

淺唱寂寞╮ 提交于 2019-12-07 21:24:56
问题 Inversion sum is not correct using Stream.reduce, what is going wrong here ? double[] array = {1.0, 2.0}; double iunversionSum = Arrays.stream(array).reduce(0.0, (a, b) -> Double.sum(1.0 / a, 1.0 / b)); output is .5 but expected is 1.5 (1/1 + 1/2) 回答1: I think using map() it could be simplier. double inversionSum = Arrays.stream(arr).map(val -> 1 / val).sum(); 回答2: The error in your reduce is: Double.sum(1.0 / a, 1.0 / b), starting the series with 0.0. Now check why your outcome is .5. Use

How to compute letter frequency in a string using pythons built-in map and reduce functions

孤人 提交于 2019-12-07 17:26:43
问题 I would like to compute the frequency of letters in a string using pythons map and reduce built-in functions. Could anyone offer some insight into how I might do this? What I've got so far: s = "the quick brown fox jumped over the lazy dog" # Map function m = lambda x: (x,1) # Reduce # Add the two frequencies if they are the same # else.... Not sure how to put both back in the list # in the case where they are not the same. r = lambda x,y: (x[0], x[1] + y[1]) if x[0] == y[0] else ???? freq =

Is it possible to iterate a stream only once and perform 2 or more operations?

给你一囗甜甜゛ 提交于 2019-12-07 17:00:48
问题 Given the code List<Integer> numbers = Arrays.asList(2, 4, 3); int sumTotal = numbers.stream().reduce(-3, (x, y) -> x + y + 3); int multiplyTotal = numbers.stream().reduce(1, (x, y) -> x * y); Is it possible to perform both operations while iterating the stream only once? Also, notice each reduce has a different identity: -3 and 1. 回答1: You can create a custom class, and use a mutable reduction: public static void main(String[] args) { List<Integer> numbers = Arrays.asList(2, 4, 3); Result

Combine/merge objects in array based upon common value

被刻印的时光 ゝ 提交于 2019-12-07 16:10:40
问题 I have an array like the following. [ { sku: 'TEA-BLCK', price: '10', quantity: '1' }, { sku: 'TEA-ELGY', price: '10', quantity: '1' }, { sku: 'TEA-CHAI', price: '10', quantity: '1' }, { sku: 'TEA-GREN', price: '10', quantity: '1' }, { sku: 'TEA-ELGY', price: '10', quantity: '1' }, { sku: 'TEA-MINT', price: '10', quantity: '1' } ] I need to make it look like this [ { sku: 'TEA-BLCK', price: '10', quantity: '1' }, { sku: 'TEA-ELGY', price: '10', quantity: '2' }, { sku: 'TEA-CHAI', price: '10',

Hadoop - Reducer is waiting for Mapper inputs?

六月ゝ 毕业季﹏ 提交于 2019-12-07 12:00:44
问题 as explained in the title, when i execute my Hadoop Program (and debug it in local mode) the following happens: 1. All 10 csv-lines in my test data are handled correctly in the Mapper, the Partitioner and the RawComperator(OutputKeyComparatorClass) that is called after the map-step. But the OutputValueGroupingComparatorClass's and the ReduceClass's functions do NOT get executed afterwards. 2. My application looks like the following. (due to space constraints i omit the implementation of the

Using array.reduce method to count duplicate elements [duplicate]

China☆狼群 提交于 2019-12-07 09:38:40
问题 This question already has answers here : Word Frequency Count, fix a bug with standard property (3 answers) Closed 5 years ago . I'm working through a tutorial on functional JS and I'm up to a challenge requiring the use of the reduce method: Given a random array of words, output an array that shows the word plus it's word count, for instance: ['apple, 'orange, 'grape', 'apple'] -> ['apple: 2','orange: 1', 'grape: 1] I know this isn't the correct use of reduce, but here was my semi-working