Day3:集合、文件操作、函数

一、集合

1、集合是一个无序的、不重复的数据集合,它的主要作用如下:

(1)去重,把一个列表变成集合就自动去重了

(2)关系测试,测试两组数据之间的交集、差集、并集等关系

# Author:licy
list_1=[1,4,5,7,3,6,7,9]
list_1=set(list_1)#变成集合
list_2=set([2,6,0,66,22,8,4])
print(list_1,list_2)
#交集
print(list_1.intersection(list_2))
#并集
print(list_1.union(list_2))
#差集 in list_1 but not in list_2
print(list_1.difference(list_2))
print(list_2.difference(list_1))
list_3=set([1,3,7])
#子集
print(list_3.issubset(list_1))
#父集
print(list_1.issuperset(list_3))
#对称差集,其实就是先将他们并集,然后他们之间都有的要剔除
print(list_1.symmetric_difference(list_2))
#判断两个集合之间是否有交集
list_4=set([5,6,8])
print(list_3.isdisjoint(list_4))
print('--------------------------------------------')
#交集
print(list_1&list_2)
#并集
print(list_1|list_2)
#差集
print(list_1-list_2)
print(list_2-list_1)
#对称差集
print(list_1^list_2)
print('-------------增伤改查----------------')
#添加
list_1.add(999)
#添加多项
list_1.update([33,44,55])
print(list_1)
#任意删除
print(list_1.pop())
#指定删除(都不会返回删除的值,只是remove如果删除没有的元素会报错)
print(list_1.remove(55))
print(list_1.discard(22))
View Code

二、文件操作

# Author:licy
'''
#打开文件
f=open("file",'r',encoding="utf-8")#文件句柄
#读文件
data=f.read()
print(data)

#打开文件
f=open("file2",'w',encoding="utf-8")#文件句柄
#写文件
f.write('我爱北京天安门')

#追加
f=open('file2','a',encoding='utf-8')
f.write('sdfsdfsdfsdfsdf')
f.close()
'''

f=open("file2",'r',encoding='utf-8')
#读前5行
#for i in range(5):
#    print(f.readline())
'''低级循环
for index,line in enumerate(f.readlines()):
    if index==3:
        print('-------------------------')
        continue
    print(line.strip())
'''
'''
#牛逼循环,效率最高
count=0
for line in f:
    if count==3:
        print('------------------')
        count+=1
        continue
    print(line)
    count+=1
'''
f=open('file2','r',encoding='utf-8')
print(f.tell())#打印当前位置
print(f.readline())
#print(f.read(50))
print(f.tell())
f.seek(0)#回到0的位置
print(f.readline())

#打印文件编码
print(f.encoding)
View Code
# Author:licy
import sys,time
for i in range(50):
    sys.stdout.write('#')
    sys.stdout.flush()
    time.sleep(0.1)
flush例子
# Author:licy
f=open("file2",'a',encoding='utf-8')
f.truncate(11)
View Code

1、读写and写读

# Author:licy
#这两个臭傻逼就卖法返回去修改
'''
f=open("file2",'r+',encoding='utf-8')#读写
print(f.readline())
f.write('---------------aa--------------')
print(f.readline())
f.close()
'''
f=open('file2','w+',encoding='utf-8')#写读
f.write('----------------diao----------------1\n')
f.write('----------------diao----------------1\n')
f.write('----------------diao----------------1\n')
f.write('----------------diao----------------1\n')
print(f.tell())
f.seek(2)
print(f.readline())
f.write('shabishabishabi')
f.close()
View Code

2、读、写二进制:

# Author:licy
#
'''
f=open('file2','rb')
print(f.readline())
print(f.readline())
print(f.readline())
'''
#
f=open('file2','wb')
f.write('shabialex'.encode())
f.close()
View Code

3、文件修改

步骤:按行读取源文件内容,判断是否为修改内容,如果是,替换之后再将他写入到新文件中,新文件就是替换后的

我草你妈了傻逼alex,装逼货
大傻逼大傻吊
傻逼alex,老男孩儿
旧文件file2

新文件file1

# Author:licy
f=open('file2','r',encoding='utf-8')
f_new=open('file','w',encoding='utf-8')
for line in f:
    if '大傻逼大傻吊'in line:
        line=line.replace('大傻逼大傻吊','李杰')
    f_new.write(line)
f.close()
f_new.close()
具体步骤

4、为了避免打开文件后忘记关闭,可以通过管理上下文,即:with

# Author:licy
with open('file','r',encoding='utf-8') as f:
    for line in f:
        print(line)
View Code

三、函数

1、面向对象:类-------->class

2、面向过程:过程---------->def

(1)定义:没有返回值的函数而已,返回None

# Author:licy
#定义函数
def func1():
    "testing"
    print('in the func1')
    return 0
#定义过程
def func2():
    "testing2"
    print('in the func2')
x=func1()
y=func2()
print('from func1 return is %s' %x)
print('from func2 return is %s' %y)

3、函数式编程:函数-------->def   其他函数式编程语言:lisp、hashsheel、erlang

(1)定义:

# Author:licy
def test(x):
    "The function definitions"
    x+=1
    return x
def:定义函数的关键字
test:函数名
():内可定义形参
"":文档描述(非必要,但是强烈建议为函数添加描述信息)
x+=1:泛指代码块或程序处理逻辑
return:定义返回值

(2)为什么要使用函数:避免写重复代码、方便扩展、保持一致性

(3)函数的返回值:

# Author:licy
def test1():
    pass
def test2():
    return 0
def test3():
    return 0,10,'hello',['alex','b'],{'name':'lcy'}

x=test1()#返回None
y=test2()#返回0
z=test3()#返回(0,10,'hello',['alex','b'],{'name':'lcy'})
print(x)
print(y)
print(z)
View Code

(4)参数:

# Author:licy
def test(x,y):#x y叫作形参
    print(x)
    print(y)
test(1,2)#1 2叫作实参 必须与形参一一对应,多一不可,缺一也不可
test(y=1,x=2)#关键字调用,与形参顺序无关
#test(x=2,3)#关键参数是不能写在位置参数前面的
test(3,y=2)
关键字参数和位置参数
# Author:licy
def test(x,y=2):#y为默认参数
    print(x)
    print(y)
test(1)
test(1,y=3)
#默认参数特点:调用函数的时候默认参数非必须传递
#默认参数的用途:需要提前赋默认值的操作
默认参数
# Author:licy
def test(*args):#参数组"*任意变量名"
    print(args)
test(1,2,3,4,5)#返回一个元组
test(*[1,2,3,4,5])#返回一个元组
参数组
# Author:licy
def test(x,*args):
    print(x)
    print(args)
test(1,2,3,4,5)
参数组和位置参数结合
# Author:licy
#**kwargs:把n个关键字参数转换成字典的方式
def test1(**kwargs):
    print(kwargs)
    print(kwargs['name'])
test1(name="licy",age=8)
test1(**{'name':'licy','age':8})
View Code
# Author:licy
def test1(name,**kwargs):#位置参数和字典参数结合
    print(name)
    print(kwargs)
test1('licy',age="licy2")

def test4(name,age=8,**kwargs):#参数组必须在最后
    print(name)
    print(age)
    print(kwargs)
test4('licy',sex="",age=3)

def test5(name,age=8,*args,**kwargs):#*args:只能接收n个位置参数,转换成元组的形式
    print(name)
    print(age)
    print(args)
    print(kwargs)
test5('licy',age=3,sex="m",grade=44)
View Code
# Author:licy
def test1(name,**kwargs):
    print(name)
    print(kwargs)
    logger('licy')
def logger(source):
    print("from %s" % source)
test1('licy',age="licy2")#程序的执行时从上到下的,所以此调用必须放在最后
特殊例子

(5)局部变量:

# Author:licy
school="Japan"#全局变量
def change_name(name):
    global school #我要在函数中改这个全局变量
    school="zhuolu"
    print("before change",name,school)
    name="Licy"#局部变量,只在函数中生效,这个函数就是这个变量的作用域
    print("after change",name)
name="licy"
change_name(name)
print(name)
print(school)
View Code

扩展知识:ctrl+? pycharm注释选中的全部代码

# Author:licy
names=["licy","gxn"]
def change():
    names[0]="Licy"#列表、字典、集合是可以在函数内部修改的
    print("函数内部:",names)
change()
print(names)
扩展

(6)递归:

# Author:licy
def cal(n):
    print(n)
    if int(n/2)>0:
        return cal(int(n/2))
cal(10)
View Code

(7)高阶函数:

# Author:licy
def add(a,b,f):
    return f(a)+f(b)
print(add(3,-6,abs))
View Code
posted @ 2017-07-23 22:09  licy_python  阅读(158)  评论(0编辑  收藏  举报