python之路——16

王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594

学习内容

1.内置函数
1. python 数据类型:int bool
数据结构:dic list tuple set str
2. reversed 反转
3. slice 切片
4. format
5. bytes 转换成bytes类型 编码转换用
网络编码 只能用二进制
照片和视频以二进制存储
html网页爬取到的也是编码
6. bytearray bytes未完整字节修改不方便,bytearray类似于列表,修改方便
7. memoryview
8. ord 字符按照unicode转数字
9. chr 数字按照unicode转字符
10. ascii
11. repr (%r格式化输出)让数据原封不动输出
12. dict set
13. frozenset(不可变集合)
14. all 可迭代的 与运算; any 可迭代的 或运算
15. zip 拉链
16. filter 只筛选,不改变值
map 值可能变换,个数不变
17. sorted 排序
2.匿名函数
为实现简单功能,一行写成的函数

代码区

1.关于bytes 代码转换

print('你好')
print(bytes('你好', encoding='GBK')) # unicode 转换成GBK的bytes
print(bytes('你好', encoding='utf-8')) # unicode 转换成utf-8的bytes
print(bytes('你好', encoding='utf-8').decode('utf-8'))

2.filter 过滤非字符串

def is_odd(x):
if type(x) == type('s'):
return x
ret = filter(is_odd, [0,'asd','1 ', '',None,4,5,6,7,8,9])
print(ret)
for i in ret:
print(i)

3.利用filter过滤1-100中开平方为整数的数

from math import sqrt
def func(num):
res = sqrt(num)
return res%1 == 0 def fun(num):
res = sqrt(num)
res_int = int(res)
return res_int == res
ret = filter(fun,range(1,101))
for i in ret:
print(i)

4.,max min filter map sorted  ——lambda 合作

# 求字典值最大值 max+lambda
'''
def func(key):
return dic[key]
dic = {'k1':10,'k2':1000,'k3':120}
print(max(dic,key=func))
print(max(dic,key=lambda k:dic[k]))
''' # 求列表元素的平方 map+lambda
'''
ret = map(lambda x:x**2,[1,3,5,7,9])
print(ret)
for i in ret:
print(i)
''' # 筛选列表中大于10的数
'''
ret = filter(lambda x:x>10,[1,3,4,6,8,4110,1,223,121])
print(ret)
for i in ret:
print(i)
'''

5.员工大礼包

# 我的答案,这个是关键dic = {'id': 0, 'name': 1, 'age': 2, 'tel': 3, 'job': 4, }
'''
def get_line(filename):
with open(filename,encoding='utf-8') as f1:
for line in f1:
line = line.strip()
line_lis = line.split(',')
yield line_lis # 接受用户信息
ret = 'select name,job where tel like 133'
view, condition = ret.split('where')
view = view.replace('select', ' ').strip()
view_list = view.split(',')
print(view_list, condition)
dic = {'id': 0, 'name': 1, 'age': 2, 'tel': 3, 'job': 4, }
def con_filter(condition):
g = get_line('log')
for i in g:
if '>' in condition:
cond_l = condition.split('>')
if int(i[dic[cond_l[0].strip()]]) > int(cond_l[1]):
if view_list[0].strip() == '*':
print(i)
else:
for j in view_list:
print('%s: %s' %(j, i[dic[j.strip()]]), end=',')
print(' ')
elif '<' in condition:
cond_l = condition.split('<')
if int(i[dic[cond_l[0].strip()]]) < int(cond_l[1]):
if view_list[0].strip() == '*':
print(i)
else:
for j in view_list:
print('%s: %s' % (j, i[dic[j.strip()]]), end=',')
print(' ')
elif '=' in condition:
cond_l = condition.split('=')
if int(i[dic[cond_l[0].strip()]]) == int(cond_l[1]):
if view_list[0].strip() == '*':
print(i)
else:
for j in view_list:
print('%s: %s' % (j, i[dic[j.strip()]]), end=',')
print(' ')
elif 'like' in condition:
cond_l = condition.split('like')
if cond_l[1].strip() in i[dic[cond_l[0].strip()]]:
if view_list[0].strip() == '*':
print(i)
else:
view_list.append('tel')
for j in view_list:
print('%s: %s' % (j, i[dic[j.strip()]]), end=',')
# print('tel:%s'%i[dic['tel']], end=',')
print(' ')
con_filter(condition)
'''
# 老师答案,做到解耦,分步操作,注释
'''
# 接收用户信息——分析信息
ret = 'select name,job, age where tel like 133'
view, condition = ret.split('where')
view = view.replace('select', ' ').strip()
view_list = view.split(',')
condition = condition.strip()
print(view_list, condition)
# 对照表
dic = {'id': 0, 'name': 1, 'age': 2, 'tel': 3, 'job': 4, }
# 读取文件——将文件信息整理到内存
def get_line(filename):
with open(filename, encoding='utf-8') as f1:
for line in f1:
line = line.strip()
line_list = line.split(',')
yield line_list
# 筛选员工——根据信息筛选内存中提取的信息
def condition_filter(condition):
g = get_line('log')
for line_list in g:
if '>' in condition:
col, val = condition.split('>')
if int(line_list[dic[col]]) > int(val):
yield line_list
elif '<' in condition:
col, val = condition.split('<')
if int(line_list[dic[col]]) < int(val):
yield line_list
elif '=' in condition:
col, val = condition.split('=')
if int(line_list[dic[col]]) == int(val):
yield line_list
elif 'like' in condition:
col, val = condition.split('like')
col = col.strip()
val = val.strip()
if val in line_list[dic[col]]:
yield line_list
# 显示——根据筛选结果,显示需要的信息
def viewer(view_list, staff_g):
for staff_info in staff_g:
for i in view_list:
print('%s:%s' % (i.strip(), staff_info[dic[i.strip()]]), end=',')
print(' ') g = condition_filter(condition)
viewer(view_list, g)
'''
上一篇:页面每隔n分钟轮换一个微信名和微信名


下一篇:NCO