defsquare_it_func(a):return a * a
x =map(square_it_func,[1,4,7])print(x)print(list(x))defmultiplier_func(a, b):return a * b
x =map(multiplier_func,[1,4,7],[2,5,8])print(x)print(list(x))
<mapobject at 0x00000000074C4048>[1,16,49]<mapobject at 0x00000000074C4A88>[2,20,56]
2、filter()
Init signature:filter(self,/,*args,**kwargs)
Docstring:filter(function orNone, iterable)-->filterobject
Return an iterator yielding those items of iterable for which function(item)is true. If function isNone,return the items that are true.
filter 内置函数与 map 函数非常相似,它也将函数应用于序列结构(列表、元组、字典)。
二者的关键区别在于 filter()将只返回应用函数返回 True 的元素。
# Our numbers
numbers =[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]# Function that filters out all numbers which are odddeffilter_odd_numbers(num):returnTrueif num %2==0elseFalse
filtered_numbers =filter(filter_odd_numbers, numbers)print(filtered_numbers)print(list(filtered_numbers))
<filterobject at 0x00000000074B8C88>[2,4,6,8,10,12,14]
对可迭代对象进行遍历,返回一个迭代器
function 参数是一个参数的函数,且返回值应当是 bool 类型,或其返回值等效于 bool 值
Signature:sorted(iterable,/,*, key=None, reverse=False)
Docstring:
Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order,and the
reverse flag can be set to request the result in descending order.