#1. lambda函数的使用
anli_1=map(lambda x:x.capitalize(),names)
#2.map 转list
print(list(anli_1))
names=['bob','mary','amy','xueyan','dahai']
age=[11,22,33,44,55]
sex=['男','女','男','女','男']
#3. 该方法返回一个首字母大写的字符串。
anli_1=map(lambda x:x.capitalize(),names)
print(list(anli_1))
#4. 将3个列表合并成一个列表,元素用元组替换
anli_2=map(lambda x,y,z:(x,y,z),names,age,sex)
list_1=list(anli_2)
print(list_1)
#5.filter 过滤函数的使用 filter(function, iterable)
参数
- function -- 判断函数。
- iterable -- 可迭代对象。
返回值
返回列表。
def is_odd(n):
return n % 2 == 1
newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(list(newlist))
def lianxi_1(x): if x[2]=='女': return True anli_3 = filter(lianxi_1,list_1) print(list(anli_3))
#6. reduce() 函数会对参数序列中元素进行累积。
reduce(function, iterable[, initializer])
参数
- function -- 函数,有两个参数
- iterable -- 可迭代对象
- initializer -- 可选,初始参数
from functools import reduce
str1 = 'the quick brown fox'
str2 = ' jumps over '
str3 = 'the lazy dog.'
print(reduce(lambda a, b: a+b, [str1, str2, str3]))
def lianjie(a,b):
return a+b
print(reduce(yunsuan,['aa','bb','cc']))