Linked Questions

22 votes
3 answers
33k views

Reduce function with three parameters [duplicate]

How does reduce function work in python3 with three parameters instead of two. So, for two, tup = (1,2,3) reduce(lambda x, y: x+y, tup) I get this one. This would just sum up all the elements in tup. ...
chanpkr's user avatar
  • 915
0 votes
5 answers
751 views

python reduce function returns false for check same element in a list [duplicate]

>>> reduce(lambda x,y: x == y, [2,2,2,2,2]) False I want to check every element in a list if they are the same and I thought this would do the trick, apprarently not. Can you some explain why ...
user avatar
8 votes
6 answers
3k views

Is it possible to sort a list with reduce?

I was given this as an exercise. I could of course sort a list by using sorted() or other ways from Python Standard Library, but I can't in this case. I think I'm only supposed to use reduce(). from ...
afaf12's user avatar
  • 5,446
8 votes
5 answers
3k views

Create dynamic level nested dict from a list of objects?

I am trying to turn a list of objects into a nested dict which could be accessed by indexes. The following code works for a two-level nested dictionary. I would like to extend it to work flexibly for ...
ChaimG's user avatar
  • 7,582
2 votes
4 answers
299 views

behaviour of reduce function in python

I am confused by the behavior of the reduce function. In the first example, I get the expected result: (1-1/2) * (1-1/3) = 1/3 >>> reduce(lambda x, y: (1 - 1.0/x) * (1 - 1.0/y), [2,3]) 0....
akasolace's user avatar
  • 634
2 votes
3 answers
3k views

creating cumulative percentage from a dictionary of data

Given a dictionary (or Counter) of tally data like the following: d={'dan':7, 'mike':2, 'john':3} and a new dictionary "d_cump" that I want to contain cumulative percentages d_cump={'mike':16, 'john'...
user1183315's user avatar
1 vote
1 answer
852 views

How spark reduce works here

How does spark reduce work for this example? val num = sc.parallelize(List(1,2,3)) val result = num.reduce((x, y) => x + y) res: Int = 6 val result = num.reduce((x, y) => x + (y * 10)) res: Int ...
user0000's user avatar
  • 387
1 vote
2 answers
426 views

Recursive reduce function in Python

I'm given a sequence tuple(range(1,5)) and I am supposed to write a recursive function with reduce to calculate the product 1*2*3*4*5 = 24. I don't know how to do this recursively. I've tried looking ...
OLGJ's user avatar
  • 472
0 votes
2 answers
125 views

Nested loop on a list with a function

I got stuck on that: a list y = (1,2,3...) a function func(A,B) a constant C How can I express this situation with loops? B1 = func(C , y[0]) B2 = func(B1 , y[1]) B3 = func(B2 , y[2]) #.... and so ...
Paradoxks's user avatar
0 votes
2 answers
129 views

Combine two pandas dataframes adding corresponding values pt2

So i have these CSV files I want to combine as follows: file1.csv Date,Time,Unique1,Common blah,blah,55,92 file2.csv Date,Time,Unique2,Common blah,blah,12,25 I want a pandas dataframe where... Date,...
oompaloompa's user avatar