Created
April 1, 2013 06:33
-
-
Save 89465127/5283497 to your computer and use it in GitHub Desktop.
Examples of how to use reduce() in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The reduce() function applies a function which will reduce the sequence to a single value. | |
# There are a number of special-purpose reduce functions that we’ve already seen. | |
# These reductions include sum(), any(), all(). | |
def red_max(x, y): | |
return x if x > y else y | |
def red_sum(x, y): | |
return x + y | |
def main(): | |
ints = [1,25,3,55,37,4] | |
print "input: {}".format(ints) | |
print "find max using reduce:" | |
print reduce(red_max, ints) | |
print "find sum using reduce:" | |
print reduce(red_sum, ints ) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment