Python之路----内置函数补充与匿名函数

时间:2023-12-05 21:01:02

内置函数补充:
reversed()
保留原列表,返回一个反向的迭代器

l = [1,2,3,4,5]
l.reverse()
print(l)
l = [1,2,3,4,5]
l2 = reversed(l)
print(l2)
l = (1,2,23,213,5612,342,43)
sli = slice(1,5,2)
print(l[sli])
print(l[1:5:2]) print(format('test', '<20'))
print(format('test', '>40'))
print(format('test', '^40'))

bytes 转换成bytes类型

我拿到的是gbk编码的,我想转成utf-8编码
print(bytes('你好',encoding='GBK')) # unicode转换成GBK的bytes
print(bytes('你好',encoding='utf-8')) # unicode转换成utf-8的bytes

网络编程 只能传二进制
照片和视频也是以二进制存储
html网页爬取到的也是编码

b_array = bytearray('你好',encoding='utf-8')
print(b_array)
print(b_array[0])
'\xe4\xbd\xa0\xe5\xa5\xbd'
s1 = 'alexa'
s2 = 'alexb'

l = 'ahfjskjlyhtgeoahwkvnadlnv'
l2 = l[:10]

切片 —— 字节类型 不占内存
字节 —— 字符串 占内存

print(ord('好'))
print(ord(''))
print(chr(97))
print(ascii('好'))
print(ascii(''))
name = 'egg'
print('你好%r'%name)
print(repr(''))
print(repr(1))
print(all(['a','',123]))
print(all(['a',123]))
print(all([0,123]))
print(any(['',True,0,[]]))
l = [1,2,3,4,5]
l2 = ['a','b','c','d']
l3 = ('*','**',[1,2])
d = {'k1':1,'k2':2}
for i in zip(l,l2,l3,d):
print(i)
def is_odd(x):
return x % 2 == 1
def is_str(s):
return s and str(s).strip()
ret = filter(is_odd, [1, 6, 7, 12, 17])
ret = filter(is_str, [1, 'hello','',' ',None,[], 6, 7, 'world', 12, 17])
print(ret)
for i in ret:
print(i)
[i for i in [1, 4, 6, 7, 9, 12, 17] if i % 2 == 1]
from math import sqrt
def func(num):
res = sqrt(num)
return res % 1 == 0
ret = filter(func,range(1,101))
for i in ret:
print(i)
ret = map(abs,[1,-4,6,-8])
print(ret)
for i in ret:
print(i)
filter 执行了filter之后的结果集合 <= 执行之前的个数
filter只管筛选,不会改变原来的值
map 执行前后元素个数不变,值可能发生改变
l = [1,-4,6,5,-10]
# l.sort(key = abs) # 在原列表的基础上进行排序
# print(l)
print(sorted(l,key=abs,reverse=True)) # 生成了一个新列表 不改变原列表 占内存
print(l)
l = [' ',[1,2],'hello world']
new_l = sorted(l,key=len)
print(new_l)

匿名函数

def add(x,y):
return x+y
add = lambda x,y:x+y
print(add(1,2))
dic={'k1':10,'k2':100,'k3':30}
def func(key):
return dic[key]
print(max(dic,key=func)) #根据返回值判断最大值,返回值最大的那个参数是结果
print(max(dic,key=lambda key:dic[key]))
max([1,2,3,4,5,-6,-7],key=abs)
ret = map(abs,[-1,2,-3,4])
for i in ret:
print(i)
def func(x):
return x**2
ret = map(func,[-1,2,-3,4])
for i in ret:
print(i)
ret = map(lambda x:x**2,[-1,2,-3,4])
def func(x):
return x>10 res = filter(func,[5,8,11,9,15])
for i in res:
print(i)

min max filter map sorted —— lambda

d = lambda p:p*2
t = lambda p:p*3
x = 2
x = d(x) #x = 4
x = t(x) #x = 12
x = d(x) #x = 24
print(x)

map方法的应用

ret = zip((('a'),('b')),(('c'),('d')))
ret = map(lambda t:{t[0]:t[1]},ret)
print(list(ret))

现有两元组(('a'),('b')),(('c'),('d')),
请使用python中匿名函数生成列表[{'a':'c'},{'b':'d'}]

#匿名函数 == 内置函数

# 方法一
t1=(('a'),('b'))
t2=(('c'),('d'))
# print(list(zip(t1,t2)))
print(list(map(lambda t:{t[0],t[1]},zip(t1,t2))))

# 方法二
print(list([{i,j} for i,j in zip(t1,t2)]))

#方法三
func = lambda t1,t2:[{i,j} for i,j in zip(t1,t2)]
ret = func(t1,t2)
print(ret)