内置函数
1 abs():取绝对值
print(abs(-10)) # 10
print(abs(10)) # 10
2 max,min()取最大最小值
# 参数形式1:max(ars1,args2...)
# 全部数字
# print(max(1,23,5,34,8,987,9,3,3,32,)) # 987
# 字母
# print(max('v','w','wrq','hh','sw')) # wrq
# 参数形式2:max(iterable)
# print(max([12,4,5,7,2,6,97])) # 97
# print(max([12,4,5,7,2,6,97],23,5,6,12,999)) # 错误
### 指定规则查找最大值,key参数必须为函数
# 按照绝对值大小
# print(max([1,4,99,-100,43],key=abs)) # -100
#
lst = [
{"name":'wer','price':100},
{"name":'qq','price':70},
{"name":'yy','price':99},
]
# 按照name字母顺序
def func(d):
return d['name']
# 按照价钱排序
def func2(d):
return d['price']
print(max(lst,key=func))
print(max(lst,key=func2))
3 map
# 将lst转化为[1,4,9]
lst = [1,2,3]
# 方法1
# l2= []
# for i in lst:
# l2.append(i*i)
# 法2:列表推导式
# lst2 = [i**2 for i in lst]
# 法3:map
# def f(x):
# return x*x
#
# res = map(f,lst)
# res2 = map(f,lst)
# for i in res2:
# print(i) # 1 4 9
#
# lst2 = list(res) # [1,4,9]
# print(lst2)
4 filter
# 练习:过滤出列表中的所有奇数
l = list(range(1,11))
# 推导式
l2 = [i for i in l if i%2 == 1]
# filter
def f2(x):
if x % 2 == 1:
return True
else:
return False
5 zip
# for i in zip([1,2,3,4],[4,5,6,],[7,8,9]): # (1,4,7)
# print(i)
# print(list(zip([1,2,3],[4,5,6,],[7,8,9])))
# 练习:现有两个元组('a','b'),('c','d'),请生成[{'a':'c'},{'b':'d'}]格式.
# 法1
# l3 = []
# for i in zip(('a','b'),('c','d')):
# l3.append({i[0]:i[1]})
# print(l3)
# 法2
# l3 = [{i[0]:i[1]} for i in zip(('a','b'),('c','d'))]
# print(l3)