Python has built-in some very interesting and useful functions, such as filter, map, and reduce, which are all processing a set, and filter is easy to understand for filtering, map for mapping, and reduce for merging. Is the Python list method of three carriages.
1. The function of the filter function is equivalent to
Python has built-in interesting but useful functions that fully reflect the charm of Python!
Filter (function, sequence): Execute function (item) in sequence for items in sequence, and combine items with the execution result of true into a list/string/tuple (depending on the sequence type) to return:>>> Def f (x): Return X % 2! = 0 and X % 3! = 0>>> Filter (F, range (2, 25 ))[5, 7, 11, 13, 17, 19, 23]>>> De
for map () its prototype is: Map (function,sequence), which is to perform function functions on each element of the sequence sequence.
For example, the previous a,b,c = Map (Int,raw_input (). Split ()), meaning that the input a,b,c is converted to an integer. Another example:
A = [' 1 ', ' 2 ', ' 3 ', ' 4 ']print map
Python has built-in interesting but useful functions that fully reflect the charm of Python! Filter (function, sequence): Execute function (item) in sequence for items in sequence, and combine items whose execution result is True into a List/String/Tuple (depending on the sequence type) return: >>> def f (x): return x % 2! = 0 and x % 3! = 0 >>> filter (f, range (2, 25) [5, 7, 11, 13, 17, 19, 23] >>> def f
This article mainly introduces the special syntax in Python: filter, map, reduce, and lambda. This article provides code examples for this special syntax. For more information, see
Filter (function, sequence ):Execute function (item) in sequence for items in sequence, and combine items with the execution result of True into a List/String/Tuple (depending on the sequence type) to return:
The code is as fol
Usage of Map——、 us to analyze the source code of the map in PythonClass Map (object): """ Map (func, *iterables)---Map object Make a iterator that computes the function using arguments fromA function that computes an iteration from each of the parameters of an i
Map and filter are the most important bif,map in Python, the main function is to deal with each element within the collection, filter is the role of filtering the set, the specific function is as follows1 t =Lambda x:x%22 list (filter (T, range))3 [1, 3, 5, 7, 9] 4 List (map (t,range))5 [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]F
In the work, often want to use similar to the geothermal map of the way data display, but the work environment is the intranet, can not be linked to the map online, no way, had to find their own ways to search the Internet border coordinates.
After a lot of documentation and trouble, finally found the way and the way the Gold Map API is the simplest, just beginne
anonymous function Lambda usage
Use lambda in Python to create anonymous functions. A lambda is just an expression that has its own namespace and cannot access parameters outside of the free argument list or in the global namespace.
Lambda syntax
Lambda arg1,arg2 ...: expression
Lambda Expressions act as a function of sketching. Example:
>>> sum = lambda x,y:x+y
>>> sum (2,4)
6
Zip () function usage
A zip () is a built-in function of
Python built-in functions filter,map and reduce, three functions are similar, are applied to the sequence of functions, the common sequence includes list, tuple, str and so on. and three functions can be used in conjunction with a lambda expression. The following are described separately.1. FilterFilter (BOOL_FUNC,SEQ): This function functions like a filter. Call a Boolean function Bool_func to iterate thro
1. LambdaThe lambda operator or lambda function is a-to-create small anonymous functions, i.e. functions without a name.It is convenient to create a function. Like what def add (x, y): Return x+yUsing Lambda to write is Lambda X,y:x+yVery concise. This statement returns a function pointer, which you can assign, or coordinate with the map, reduce, and so on.For example, if you want to multiply each value of list [1,2,3,4,5] by 2, return a list.2. Map
The use of Map,reduce,filter in 1,python
>>> a=[1,2,3,5]
>>> map (lambda x:x+x,a)
[2, 4, 6, ten]
>>> map (lambda x:2*x,a)
[2, 4, 6,]
>>> reduce (lambda x,y:x*y,a)
>>> filter (lambda x:x%2,a)
[1, 3, 5]
The use of Map,reduce,filter in 2,scala
scala> val list2 = 1 to toli
Python converts a string to a number using reduce and map.
Introduction to reduce and map in python
map(func,seq1[,seq2...]) : Function func is used to act on each element of a given sequence, and a list is used to provide the return value. If func is None, func is represent
Introduction to reduce and map in Python
map(func,seq1[,seq2...]) : func function to each element of a given sequence, using a list to provide the return value, or, if Func is an identity function for None,func, returns a list of n tuples containing the set of elements in each sequence.
reduce(func,seq[,init]) : Func is a two-dollar function, the elements that
The map () function map () is a Python built-in high-order function that receives a function f and a list, and then, by putting the function f on each element of the list in turn, gets a new list and returns. For example, for list [1, 2, 3, 4, 5, 6, 7, 8, 9if you want to square each element of the list, you can use the map
Python reads multiple integers in a row + map () function Parsing
In python, the input raw_input can only be read into one number, but sometimes it needs to be read into multiple numbers. What should we do? Two raw_input can be used to read two numbers, but what should we do if we need to read ten numbers, you cannot use ten raw_nput statements.
import sys num1
This article mainly introduces how to use the map, reduce, filter, and sorted functions in Python. it is the basic knowledge of getting started with Python. For more information, see
Map
map(funcname, list)
The python
1.filter (BOOL_FUNC,SEQ)Filter () is a ' filtering function ', and also receives a function and a sequence, filter () acts on each element of the sequence by the function of the descendant, and then decides whether to persist or discard the element based on whether the return value is TRUE or FalseExample:
def fr (x):
return x%2==1
print ' Filter1: ' , Filter (Fr,range (1,51)) #筛选出100以内的所有奇数
Print ' filter2: ', Filter (fr,[1,2,3,4])
Output: Filter1: [1, 3, 5, 7, 9, one, ,, +
variables, the return result is a Map object address, so to use the list to cast under thePrint(RES)#Eventually, the contents of the LIS and Lis1 are looped through the My function, res=[(1, ' a ') (2, ' B ') (3, ' C ') (4, ' d ')]Third, filterFilter and map on the surface is the same, is the loop call function, the difference is: filter is filtered, it will be the content of the loop into the function, th
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.