strings:
Copy Code code as follows:
>>> map (str,[1,2,3,4,5])
[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
>>>
Just one line of code, it's done. Let's look again at the exercises from the Gushe Python tutorial: Use the map () function to change the nonstandard English name entered by the user into the first letter capital and other lowercase
Map functions in a sequence mapThe map function applies the passed-in function to each element in a sequence object, and returns a list containing the results of all function calls.Example 1:def sum (x): return x + 10l1 = [1,2,3,4,5,6,7]l = Map (sum, L1) #结果为 [11, 12, 13, 14, 15, 16, 17]Map also has a more advanced use
Several of the functions built into Python are simple examples of filter, map, reduce, and lambda.#!/usr/bin/env python#_*_coding:utf-8_*_#filter (function, sequence):#a function (item) is executed on item in sequence, and the item that executes the result of true consists of a list/string/tuple (depending on the type of sequence) returned. #can be seen as a filt
How to Use map, reduce, filter, and sorted functions in Python, pythonsorted
Map
map(funcname, list)
The python map function allows the function to pass each element of the list directly to funcname as a parameter and return a new
It was found that python has a standard function similar to the map-Reduce concept in hadoop, so it was quite interesting to come and play. Although its functionality was simple, it did everything.
Map (func, * iterables) --> map object make an iterator that computes the function using arguments from each of the i
+ yReduce (FN, [2, 4, 6, 7, 9])Results:2467905Combining map and reducedef str2num (s):Return reduce (FN, map (Char2num, s))Print (Str2Num (' 213579 '))Results:213579Explanation: The map function converts each character in the sequence s to num, returning a iterator object, and the parameter fn of reduce will convert the iterator object to the corresponding integ
This example describes the use of map, any, all functions in Python. Share to everyone for your reference. The specific analysis is as follows:
Recently want to learn Python, has been more concerned about Python, yesterday in the Python bar to see a post asked how in
g = Lambda x:x+1Take a look at the results of the execution:G (1)>>>2G (2)>>>3Of course, you can also use this:Lambda x:x+1 (1)>>>2It can be thought that lambda, as an expression, defines an anonymous function, the code x for the example above is the entry parameter, x+1 is the function body, and is represented by a function:def g (x): Return x+1Very easy to understand, where Lambda simplifies the writing of function definitions. Code is more concise, but the use of functions is more intuitive
])#[3, 4, 5]RunFun: [3, 4, 5] [3, 4, 5]3.2 Two iteration parameters (elements within an iteration parameter are equal)def Fun (x, y ): return x += map (fun,[0,1,2],[10,11,12= Map (Fun,range (3), range (10,13))Print for in Y]) #[Ten, A,]printfor in z]) #[Ten, A.]Run[10, 12, 14]3.3 Two iteration parameters (elements are not equal within an iteration parameter)When the elements in the iteration para
Python has built in some very interesting and useful functions, such as filter, map, reduce, are all processing a set, filter is easy to understand for filtering, map for mapping, reduce for merging. Is the Python list method of three carriages.
1. The function of the filter function corresponds to the filter. Call a
Self-learning python, many places need to be a bad supplement. The three functions are similar, and are all built-in functions that are applied to a sequence. Common sequences include list, tuple, and Str. The 1.map function map function maps the specified sequence according to the provided function. Definition of map
a dictionary.In [the]: d= ' zk_version\t3.4.6 ' in []: Map (str.strip,d.split (' t ')) out[99]: [' zk_version ', ' 3.4.6 ']in []: Key,value =map (Str.strip,d.split (' t ')) in [101]: keyout[101]: ' zk_version ' in [102]: valueout[102]: ' 3.4.6 ' in [103]: result[key]= Valuein [104]: resultout[104]: {' zk_version ': ' 3.4.6 '}2.zip () functionThe purpose of the zip () is to return a list of tuplesZip (seq1
(res1)
print
(res2)
"" "" ""
Filter functionFilter (function or None, iterable), the function is to filter out some elements of the list according to the defined functionclass Filter (object): """ Filter (function or None, iterable)--Filter Object Return an iterator yielding those items of iterable f or which function (item) is true. If function is None, and return the items is true. """View CodeHow to use:Flt = filter (lambda x:x > 5,range =
The main function of the map () function is to reduce the code significantly when you perform the same operation on a batch element.Detailed map function Parameters:Map (param1,param2,***)param1: function name, functions to be processed for bulk elementsPARAM2~PARAMX: An Iterative object, (list, meta-ancestor, iterator ...) In other words: can execute for the x in Param: The statement can be all)When
Recently want to learn Python, has been more concerned about Python, yesterday in Python saw a post asked how in Python how to determine whether the password conforms to the specification, there are many replies in the loop, in addition to also have a loop, the code is very concise, the following is the code:1 def Voli
Python has built in some very interesting but very useful functions, fully embodies the language charm of Python! [1] Filter (function, sequence)Execute function (item) on item in sequence, and the item that executes the result of true is composed of a list/string/tuple (depending on the type of sequence) returned:>>> def F(x): returnX%2!=0 andX%3!=0 >>>Filter (F, Range (2, -)) [5,7, One, -, -, +, at]>>> d
)ifi:res.append (i)returnResPrint(Filter_test (NOT_EMPTY,AA))#[' A ', ' B ', ' C ']#2.filter built-in function testPrint(List (filter (NOT_EMPTY,AA)))#[' A ', ' B ', ' C ']put the list of empty strings, blank elements, all removedThe key to the filter () function is to implement a filtering function correctly,Note: the filter () function returns a iterator, memory address, a value that needs to look at the memory address, a list () function, or the value of the address1.7sorted functionThe sorte
1. Lambda functionLambda () is an anonymous function in Python with the following syntax:Lambda [arg1[, Arg2, ... ArgN]: expressionWhen learning conditional operations, for simple if else statements, you can use the ternary operation to indicate that:# Common conditional Statements if 1 = =1 :'evescn'else :' GM ' # Ternary Operations ' EVESCN ' if Else ' GM 'For simple functions, there is also an easy way to represent a lambda expression# #####
and lambda is an expression under which the lambda is the only way to use the def[( Lambda x:x*x) (x) for x in range (1,11)] The function in Map,reduce,filter can be generated with a lambda expression!map (function,sequence)Pass the worth parameter in the sequence to function, and return a list containing the result of the function execution.If function has two parameters, that is,
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.