reduce

How can I reduce an operation with a subscript?

How can I reduce an operation with a subscript? Question: expenses = [ (‘Dinner’, 80), (‘Car repair’, 120), (‘csgo skins’, 200) ] # Version with for-loop sum = 0 for expense in expenses: sum += expense[1] print(sum) # Version using lambda+reduce sum2 = reduce((lambda a, b: a[1] + b[1]), expenses) print(sum2) I’m trying to obtain, …

Total answers: 2

reduce function gives unexpected result in a 2d list in python

reduce function gives unexpected result in a 2d list in python Question: hi guys I wrote this small code , and in my understanding of the reduce function this code should give me the total of the the prices but it doesn’t can any one explain what’s going on. import functools bill = [["jaket" , …

Total answers: 3

is there anyway of getting a values index in an iterable when using functools.reduce in python

is there anyway of getting a values index in an iterable when using functools.reduce in python Question: I have a function that would benefit from using functools.reduce it would act something like this import functools def add(total, val, max_index, index): if index > max_index: return total else: return total + val arr = [1,2,3,4,5,6,7,8] functools.reduce(functools.partial(add, …

Total answers: 2

Adjust the quality of a resized PNG image in PIL

Adjust the quality of a resized PNG image in PIL Question: No matter what I do to reduce the quality of the PNG image, this does not chnage the file size, it appears that it is not working with me, how can I use quality argument to reduce the quality and hence the file size …

Total answers: 2

python – Length of a list with the reduce() function

python – Length of a list with the reduce() function Question: I need some help to count the numbers of elements in a list by using the reduce function. def lenReduce(L): return reduce(lambda x: x + 1, L) With this one I get the following error message: TypeError: <lambda>() takes 1 positional argument but 2 …

Total answers: 3

"Reduce" function for Series

"Reduce" function for Series Question: Is there an analog for reduce for a pandas Series? For example, the analog for map is pd.Series.apply, but I can’t find any analog for reduce. My application is, I have a pandas Series of lists: >>> business[“categories”].head() 0 [‘Doctors’, ‘Health & Medical’] 1 [‘Nightlife’] 2 [‘Active Life’, ‘Mini Golf’, …

Total answers: 4

Reduce function doesn't handle an empty list

Reduce function doesn't handle an empty list Question: I previously created a recursive function to find the product of a list. Now I’ve created the same function, but using the reduce function and lamdba. When I run this code, I get the correct answer. items = [1, 2, 3, 4, 10] print(reduce(lambda x, y: x*y, …

Total answers: 2

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

How to use filter, map, and reduce in Python 3 Question: 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 …

Total answers: 8