abs() 去绝对值
max()求最大值 max(interable,key,default)
其中interable为跌代器,max会for i in - 遍历列表这个迭代器,然后将迭代器的每个返回值,当做参数传递给key = func 中的func(一般用lambda表达式定义),然后将func的执行结果传递给key在对key的值进行比较。
lst = [{'name':'zs','price':100},
{'name':'bl','price': 20},
{'name':'ng','price':30}]
def max_name(dic):
return dic['name'] #返回字典name对应的值
ret = max(lst,key=max_name) #指定比较内容(列表,关键字)
print(ret)
#
map()函数
两个参数第一个是函数,第二个是可迭代内容,函数依次将可迭代内容的每一个元素进行计算,返回一个新的可迭代内容。
lst =[1,3,5]
def square(num):
num2 = num*num
return num2
# ret = map(square,lst) #(函数,可迭代内容)
# print(ret)
# for i in ret:
# print(i)
#如果返回列表,我们直接可以打印变量,查看列表的内容
#如果返回一个迭代器,我们需要循环遍历这个迭代器,依次取出内容
ret = list(map(square,lst))
print(ret)
filter()函数用于过滤序列
过滤掉不符合条件的元素,返回有复合条件的元素组成新列表
接受两个参数,第一个为函数,的第二个为序列,序列的每一个元素作为参数传递给函数 返回True 或False,最后返回True
判断奇数,偶数
lst = [1,2,3,4,5,6,7]
def odd_number(num):
if num % 2== 1:
return num
ret = filter(odd_number,lst)
for i in ret:
print(i)
zap()函数
把多个迭代对象中的每个元素按顺序合成一个元组
a = [1,2,3,4]
b = ['a','b','c','d']
c = ['t','j','o']
ret = zip(a,b,c)
for i in ret:
print(i)
tup1 =(('a'),('s'))
tup2 = (('z'),('w'))
ret = zip(tup1,tup2)
for i in ret:
print(i)
tup1 =(('a'),('s'))
tup2 = (('z'),('w'))
print(list(map(lambda t: {t[0]: t[1]}, zip(tup1,tup2)))) #[{'a': 'z'}, {'s': 'w'}]
等价
tup = (('a'),('s'))
tup1=(('z'),('x'))
ret=zip (tup,tup1)
def func(tup):
return {tup[0]:tup1[1]}
lst = []
ret1=map(func,ret)
for i in ret1:
lst.append(i)
print(i)
print(lst)