map reduce in python

MapReduce is a programming paradigm commonly used in distributed computing for processing large datasets. In Python, we can use the built-in map, reduce, and filter functions to implement MapReduce. We can also use lambda functions for efficient and concise code.

Here's an example of how to use MapReduce in Python:

main.py
# Define a list of numbers
numbers = [1, 2, 3, 4, 5]

# Use map() function to calculate the square of each number
squares = list(map(lambda x: x**2, numbers))
print(squares)  # Output: [1, 4, 9, 16, 25]

# Use reduce() function to calculate the sum of all numbers
from functools import reduce  # import reduce function from functools module
sum = reduce(lambda x, y: x + y, numbers)
print(sum)      # Output: 15
412 chars
12 lines

In the example above, we define a list of numbers [1, 2, 3, 4, 5] and use the map function with a lambda function to calculate the square of each number. We then use the reduce function with another lambda function to calculate the sum of all numbers. Finally, we print the results.

By using the map and reduce functions together, we can efficiently process large datasets using functional programming techniques.

gistlibby LogSnag