python第三天

集合及其运算

#集合是无序的,去重的
list_1 = [1,2,3,3,4,4,5,6]
list_1 = set(list_1)#去除重复的元素
print(list_1)

list_2 = set([1,55,6,4,77,64])
print(list_1,list_2)

#交集
print(list_2.intersection(list_1))
print(list_1 & list_2)
#并集
print(list_1.union(list_2))
print(list_2 | list_1)
#差集
print(list_1.difference(list_2))#list_1中有的,list_2没有的
print(list_1 - list_2)
print(list_2.difference(list_1))#list_2中有的,list_1没有的
print(list_2 - list_1)
#子集
list_3 = set([1,4,6])
print(list_3.issubset(list_1))
print(list_1.issuperset(list_3))
#对称差集
print(list_1.symmetric_difference(list_2))#两方互相没有的元素
print(list_1 ^ list_2)

print('--------------------------------------')
list_4 = set([2,3,5])
print(list_3.isdisjoint(list_4))#判断是否存在交集,存在为F,不存在T

#添加
list_1.add(999)
list_1.update([888,777,666])#添加多项
print(list_1)
#print(list_1.pop())#随机删除
list_1.discard(464)#不存在是不会报错

文件读与写

#data = open('yest',encoding='utf-8').read()
#print(data)
#
f = open('yest','r',encoding='utf-8')# r为读模式  w为写模式 a为添加模式
data = f.read()#指针已经到达底部,所以没有数据
print(data)
f.close()

#
f = open('yest2','w',encoding='utf-8')
f.write('中华人民共和国\n')
f.write('河北\n')
f.close()

#添加
f = open('yest2','a',encoding='utf-8')
f.write('中华人民共和国\n')
f.write('河北\n')
f.close()

#单行输出
f = open('yest','r',encoding='utf-8')
for i in range(5):
    print(f.readline())#读取一行
f.close()

#多行输出
f = open('yest','r',encoding='utf-8')
for index,line in enumerate(f.readlines()):#读取全部组成一个列表,一行为一个元素
    if index == 9:
        print('-----分割线------')
        continue
    print(line.strip())#数字元素中含有换行符,需要strip注释掉
f.close()

#多行输出,防止内存不足,只保留一行在内存
f = open('yest','r',encoding='utf-8')
count = 0
for line in f:
    count +=1
    if count == 10:
        print('-----分割线------')
        continue
    print(line.strip())
f.close()

#指针的操作
f = open('yest','r',encoding='utf-8')
print(f.tell())#获取指针位置
print(f.readline())
print(f.readline())
print(f.readline())
print(f.tell())
f.seek(0)
print(f.readline())
f.close()

#其他
f = open('yest','a',encoding='utf-8')
print(f.encoding)#编码格式
print(f.flush())#实时刷新,将内容保存在硬盘
f.truncate()#截断没有参数则是清零,参数截取范围
f.close()

#读写  操作原文
f = open('yest','r+',encoding='utf-8')
print(f.readline())
print(f.readline())
print(f.readline())
f.write('--------------------------')
f.close()

#写读 创建新文件
f = open('yest3','w+',encoding='utf-8')
print(f.readline())
f.write('--------------------------\n')
f.write('--------------------------\n')
f.seek(5)
print(f.readline())
f.close()

#加读 除了加还可以读
f = open('yest3','a+',encoding='utf-8')
f.close()

#二进制文件,视频语音之类
f = open('yest3','rb')
print(f.readline())
print(f.readline())
f.close()

f = open('yest3','wb')
f.write('hello binary\n'.encode())
f.close()

文件修改

f = open('yest2','r',encoding='utf-8')
f_new = open('yest2.bak','w',encoding='utf-8')

for line in f:
    if '河北' in line:
        line = line.replace('河北','河北省')
    f_new.write(line)
f.close()
f_new.close()

字符编码转换

import  sys
print(sys.getdefaultencoding())
#python默认Unicode编码
s = '你好' #python3默认为utf-8。与文件都无关
s_gbk = s.encode('gbk')
#转成GBK
print(s_gbk)#gbk
print(s.encode())#utf-8

gbk_to_utf8 = s_gbk.decode('gbk').encode('utf-8')
print('utf-8',gbk_to_utf8)

函数

#函数
def func1():
    '''testing1'''
    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)
import time

def logger():
    time_format = '%Y-%m-%d %X'
    time_current = time.strftime(time_format)
    with open('a.txt','a+') as f:
        f.write('%s end action\n' %time_current)

def test1():
    print('test1 starting action...')
    logger()

def test2():
    print('test2 starting action...')
    logger()

def test3():
    print('test3 starting action...')
    logger()

test1()
test2()
test3()
def test1():
    print('in the test1')

def test2():
    print('in the test1')
    return 0

def test3():
    print('in the test2')
    return 1,'hello',['zzz','aaaa']#返回一个元组
    print('test end')#此句及其后面的都不会运行

x = test1()
y = test2()
z = test3()
print(x)
print(y)
print(z)
def test(x,y,z):
    print(x)
    print(y)
    print(z)

test(1,2,3) #与形参一一对应
test(z=3,y=2,x=1) #与形参顺序无关
test(1,z=3,y=2) #test(x=1,2,3)出错
def test(x,y=2):
    print(x)
    print(y)
    print('成功')

test(1)
test(1,y=3)
test(1,3)

#默认参数特点:调用函数的时候,默认参数非必须传递
#参数组
def test1(x,*args):
    print(x)
    print(args)

#**kwargs:把N个关键字,转换成字典的方式
def test2(**kwargs):
    print(kwargs)
    print(kwargs['name'])
    print(kwargs['age'])
    print(kwargs['sex'])

test1(1,2,3,4,5)
test1(*[1,2,3,4,5,6]) # *args = *[1,2,3,4,5,6]
test2(name='',age=18,sex='')
test2(**{'name':'li','age':8,'sex':''})

局部变量和全局变量

school = '全局变量1'
names = ['li','zhang','wang']
def change_name(name):
    global school #方法内更改全局变量需要声明
    print('before change',name)
    name = 'LI'
    print('before change',name)
    school = '全局变量2'#如果没有声明,则为正常的局部变量

    names[0] = 'lisi'#字符串,整数以外的数据类型的全局变量,可以修改

name = 'li'
change_name(name)
print(school)
print(names)

递归

1.必须有一个明确的结束条件

2.每次进入更深一层,问题规模相比上一次应有所减少

3.递归效率不高

def calc(n):
    print(n)
    if int(n/2) > 0:
        return calc(int(n/2))
    print('-->',n)
#最大递归次数999

calc(10)

 

posted @ 2018-02-13 20:08  猪头来袭  阅读(269)  评论(0编辑  收藏  举报