Skip to content

Instantly share code, notes, and snippets.

@89465127
Created April 1, 2013 06:33
Show Gist options
  • Save 89465127/5283497 to your computer and use it in GitHub Desktop.
Save 89465127/5283497 to your computer and use it in GitHub Desktop.
Examples of how to use reduce() in python
# 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