码迷,mamicode.com
首页 > 其他好文 > 详细

Map, filter and reduce

时间:2014-07-16 19:29:14      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   os   2014   

To add up all the numbers in a list, you can use a loop like this:

bubuko.com,布布扣                       

Total is initialized to 0. Each time through the loop, x gets one element from the list. the += operator provides a short way to update a variable:

Total += x is equivalent to: total = total + x

As the loop executes, total accumulates the sum of the elements; a variable used this way is sometimes called an accumulator. Adding up the elements of a list is such a common operation that Python provides it as a built-in function, sum:

bubuko.com,布布扣 

An operation like this that combines a sequence of elements into a single value is sometimes called reduce. Sometimes you want to traverse one list while building another. For example, the following function takes a list of strings and returns a new list that contains capitalized strings:

 bubuko.com,布布扣

res is initialized with an empty list; each time through the loop, we append the next element. So res is another kind of accumulator. An operation like capitalize_all is sometimes called a map because it ‘maps’ a function (in this case the method capitalize) onto each of the elements in a sequence.

Another common operation is to select some of the elements from a list and return a sublist. For example, the following function takes a list of strings and returns a list that contain only the uppercase strings:

 bubuko.com,布布扣

isupper is a string method that returns True if the string contains only upper case letters. An operation like only_upper is called a filter because it selects some of the elements and filters out the others.

Most common list operations can be expressed as a combination of map, filter and reduce. Because these operations are so common, Python provides language features to support them, including the built-in function reduce and an operator called a ‘list comprehension’. But these features are idiomatic to Python.

 bubuko.com,布布扣

Another simple way:

 bubuko.com,布布扣

 

list comprehension

A compact way to process all or part of the elements in a sequence and return a list with the results. result = [‘{:#04x}‘.format(x) for x in range(256) if x % 2 == 0] generates a list of strings containing even hex numbers (0x..) in the range from 0 to 255. The if clause is optional. If omitted, all elements in range(256) are processed.

 

 

from Thinking in Python

 

Map, filter and reduce,布布扣,bubuko.com

Map, filter and reduce

标签:des   style   blog   http   os   2014   

原文地址:http://www.cnblogs.com/ryansunyu/p/3841272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!