python3 map function and a filter function Detailed

This article describes the python3 map function and a filter function Detailed, paper, sample code described in great detail, has a certain reference value of learning for all of us to learn or work, a friend in need can refer to the
map () function can be data for a perform the same iteration. E.g:

def f(x):
   return x * x
r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
print(list(r))

map function passed in the first parameter is a function itself, that is, f. The second argument is the data to be operated

map () as higher-order functions, in fact it is the abstract rules of operation, and therefore, we can not only calculate simple f (x) = x 2, can also calculate any complex function, for example, put all the numbers into this list string:

print(list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9])))

Action filter () is a sequence from a qualified sieve element.

grammar:

Grammar filter is a filter function (function name, sequence)

list1=[20,12,13,5,8,19]
 def fa(n):#过滤出大于10的数字,并生成一个新的列表  list2=[]  if n>10:    list2.append(n)  return list2
print(list(filter(lambda n:n>10,list1)))#使用filter配合lambda函数实现输出结果:[20,12,13,19]

map function and filter function can be used in conjunction with lambda function, a sequence can be achieved on each element does the same thing

We recommend the python learning sites to see how seniors are learning! From basic python script, reptiles, django, data mining, programming techniques, as well as to combat zero-based sorting data items, given to every love learning python small partner! Python veteran day have to explain the timing of technology, to share some of the ways to learn and need to pay attention to small details, click on Join us python learner gathering
is more than the entire contents of this article, we want to help learning

Published 13 original articles · won praise 0 · Views 8693

Guess you like

Origin blog.csdn.net/haoxun06/article/details/104383632