D18-01 复习 map reduce fliter

map函数(处理可迭代对象),

map函数相对于for循环,将对象l交给str函数处理,map直接处理的结果为内存地址,需要用list转换

以下是将列表转换成字符串 str

l=[1,2,3,4,5]
print(list(map(str,l)))
打印结果
['1', '2', '3', '4', '5']

  

reduce函数 一定要先引用 从函数库中导入 from funtools import reduce

reduce 相当于合并到一起

l=[1,2,4,5]
from functools import reduce
res = reduce(lambda x,y:x+y,l,3)  #此处3为初始值,可以不设置
print(res)
打印结果15

  

  fliter函数 过滤  

name = ['liu','zhang']
res = filter(lambda x:x.endswith('g'),name)   ##not endswith
print(list(res))
结果
['zhang']

  文件操作

基本流程

打开,处理,关闭

f = open('梁静茹','r',encoding='utf-8')
data = f.read()
print(data)
f.close()

  

w 模式相当于覆盖 

a 模式 是追加 直接在文件最后打开 f.write(‘要写的内容’)

r+ 模式 读加写模式 从最开始开始写

 

posted @ 2018-08-23 16:09  犀利的攻城狮  阅读(102)  评论(0编辑  收藏  举报