Python第二模块(文件和函数)

1. 集合操作                                                                                    

集合的特点:无序,不重复的数据组合

集合的作用:

  • 去重,将列表变为集合,就会自动去重
  • 关系测试,测试两组数据之间的交集、差集、并集关系

常用操作:

#创建集合
s = {1,2,35,6}
#将列表变为集合
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))

#判断两个集合之间有没有交集部分
print("-------------")

list_4 = set([5,6,7,8])
print(list_3.isdisjoint(list_4)) # Return True if two sets have a null intersection.

也可以用符号来取代函数:
a = t | s          # t 和 s的并集  
  
b = t & s          # t 和 s的交集  
  
c = t – s          # 求差集(项在t中,但不在s中)  
  
d = t ^ s          # 对称差集(项在t或s中,但不会同时出现在二者中)
#例如下面的例子:
#交集
print(list_1 & list_2)
#union(并集)
print(list_2 | list_1)

#difference(差集)
print(list_1 - list_2) # in list 1 but not in list 2

#对称差集
print(list_1 ^ list_2)

#其他操作:

#添加:
t.add('x)   #添加一项
s.update([2,3,654])  #添加多项

#删除
t.remove('h')   #删除一项,如果这一项不存在就会报错
t.pop()    #随机删除一项
t.discard()    #删除特定的一项,如果这一项不存在不会报错,会返回一个None

#求长度
len(s)

#判断x是否是集合s的成员
x in s
x not in s

#判断s是否是t的子集,返回 true or false
s.issubset(t)

#判断s是否是t的父集 true or false
s.issupperset(t)

#判断两者是否有关系,没有关系则返回true,有关系则返回false
isdisjoint()

#浅复制
s.copy()   #返回s的一个浅复制
View Code

2.文件操作

对文件的操作流程:

1.打开文件,得到文件句柄并赋值给一个变量

2.通过句柄对文件进行操作

3.关闭文件

例:

文件如下:

 1 Somehow, it seems the love I knew was always the most destructive kind
 2 不知为何,我经历的爱情总是最具毁灭性的的那种
 3 Yesterday when I was young
 4 昨日当我年少轻狂
 5 The taste of life was sweet
 6 生命的滋味是甜的
 7 As rain upon my tongue
 8 就如舌尖上的雨露
 9 I teased at life as if it were a foolish game
10 我戏弄生命 视其为愚蠢的游戏
11 The way the evening breeze
12 就如夜晚的微风
13 May tease the candle flame
14 逗弄蜡烛的火苗
15 The thousand dreams I dreamed
16 我曾千万次梦见
17 The splendid things I planned
18 那些我计划的绚丽蓝图
19 I always built to last on weak and shifting sand
20 但我总是将之建筑在易逝的流沙上
21 I lived by night and shunned the naked light of day
22 我夜夜笙歌 逃避白昼赤裸的阳光
23 And only now I see how the time ran away
24 事到如今我才看清岁月是如何匆匆流逝
25 Yesterday when I was young
26 昨日当我年少轻狂
27 So many lovely songs were waiting to be sung
28 有那么多甜美的曲儿等我歌唱
29 So many wild pleasures lay in store for me
30 有那么多肆意的快乐等我享受
31 And so much pain my eyes refused to see
32 还有那么多痛苦 我的双眼却视而不见
33 I ran so fast that time and youth at last ran out
34 我飞快地奔走 最终时光与青春消逝殆尽
35 I never stopped to think what life was all about
36 我从未停下脚步去思考生命的意义
37 And every conversation that I can now recall
38 如今回想起的所有对话
39 Concerned itself with me and nothing else at all
40 除了和我相关的 什么都记不得了
41 The game of love I played with arrogance and pride
42 我用自负和傲慢玩着爱情的游戏
43 And every flame I lit too quickly, quickly died
44 所有我点燃的火焰都熄灭得太快
45 The friends I made all somehow seemed to slip away
46 所有我交的朋友似乎都不知不觉地离开了
47 And only now I'm left alone to end the play, yeah
48 只剩我一个人在台上来结束这场闹剧
49 Oh, yesterday when I was young
50 噢 昨日当我年少轻狂
51 So many, many songs were waiting to be sung
52 有那么那么多甜美的曲儿等我歌唱
53 So many wild pleasures lay in store for me
54 有那么多肆意的快乐等我享受
55 And so much pain my eyes refused to see
56 还有那么多痛苦 我的双眼却视而不见
57 There are so many songs in me that won't be sung
58 我有太多歌曲永远不会被唱起
59 I feel the bitter taste of tears upon my tongue
60 我尝到了舌尖泪水的苦涩滋味
61 The time has come for me to pay for yesterday
62 终于到了付出代价的时间 为了昨日
63 When I was young
64 当我年少轻狂
View Code

基本操作:

f = open('music',encoding="utf-8")
first_line = f.readline()
print('first line:',first_line)  #读一行
print('我是分割线'.center(50,'-'))
data = f.read() #读取剩下的所有内容,文件太大不宜使用
print(data)#打印文件

f.close() #关闭文件
  

 打开文件的模式有如下几种: 

  • r,  只读模式(默认)
  • w, 只写模式【不可读;不存在则创建;存在则删除内容】
  • a,追加模式【可读;不存在则创建;存在则追加内容】

“+”表示同事读写某个文件

  • r+,可读写文件【可读;可写;可追加】
  • w+,写读文件
  • a+,同a【可读;不存在则创建;存在则追加内容】

“b”表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件需要标注)

  • rb
  • wb
  • ab

常用操作:

1 f = open('music',encoding="utf-8")
2 for index, line in enumerate(f.readlines()):
3     if index == 9:
4         print("------------------------我是分割线---------------------")
5         continue
6     print(line.strip())

(不推荐)上述这种方法比较占用内存,因为f.readlines()只适合读取小文件,并且是放到内存里面读取的

 (推荐)用下面这个方法效率比较快,因为是一句一句读,并且打印输出

1 count = 0
2 for line in f:
3     count += 1
4     if count == 2:
5         print('----我是分割线----------')
6         # count += 1
7         continue
8     print(line)
#“+”的表示形式
f = open("yesterday2",'r+',encoding="utf-8") #文件句柄 读写
f = open("yesterday2",'w+',encoding="utf-8") #文件句柄 写读
f = open("yesterday2",'a+',encoding="utf-8") #文件句柄 追加读写

#二进制文件的表示形式:

f = open("yesterday2",'wb') #文件句柄  二进制文件
f.write("hello binary
".encode())  #将字符串encoding成二进制文件
f.close()

另外:print(f,type(f)) 打印输出的是字符串的形式

print(f.readlines())打印输出的是列表形式

如果要进行修改文件:

 1 #先打开两个文件,其修改之后的文件在yesterday2.bak里面
 2 f = open("yesterday2","r",encoding="utf-8")
 3 f_new = open("yesterday2.bak","w",encoding="utf-8")
 4 
 5 for line in f:
 6     if "有那么多肆意的快乐等我享受" in line:
 7         line = line.replace("肆意的快乐等我享受","肆意的快乐等Alex享受")
 8     f_new.write(line)
 9 f.close()
10 f_new.close()

 进度条:

import sys,time

for i in range(20):
    sys.stdout.write("#")
    sys.stdout.flush()
    time.sleep(0.1)

另外还有其他函数:

f.seek() 移动光标到哪个字符位置

f.tell() 总共有多少个字符  

f.encoding()

f.errors

f.flush()     缓存满了就存在内存里面

f.buffer()     强制刷新内存

dir(f.buffer()) 

 with语句:

为了避免打开文件后忘记关闭,可以通过管理上下文,即:
import
sys with open("yesterday2","r","encoding="utf-8" as f, open("yesterday2","r","encoding="utf-8"as f2: for line in f: print(line)
当with代码块执行完毕时,内部会自动关闭并释放文件资源。

 编码解码:

说明:

1.Python3中默认的是utf-8,Python2默认是Ascii码

2.unicode分为uft-32(4个字节),utf-16(占2个字节),utf-8(占1-4个字节)。所以:utf-8就是unicode

3.在python3中encode,转码同事还会把string类型变为bytes类型,decode解码同时会将bytes类型变回string类型

4.gbk→ utf-8:先将gbk编码decode为Unicode(decode(gbk)),再encode为utf-8

 utf-8→ gbk:先将utf-8编码decode为unicode(decode(utf-8)),再encode为gbk

练习:

s = "你好"    #unicode类型
s_gbk = s.encode("gbk")  #gbk

print(s_gbk)
print(s.encode())        #utf8
print(s.encode("utf-8"))

gbk_to_utf8 = s_gbk.decode("gbk").encode("utf-8")
print("utf8",gbk_to_utf8) 
1 s = "你好"   #unicode类型
2 print(s.encode("gbk"))
3 print(s.encode("utf-8"))
4 print(s.encode("utf-8").decode("utf-8").encode("gb2312"))
5 print(s.encode("utf-8").decode("utf-8"))

3.函数

函数的特性:

  1. 减少重复代码
  2. 使程序变的可扩展
  3. 使程序变得易维护

函数要有返回值,如果没有指定返回值,则返回none

 语法定义:

def hello():  #函数名
    print("hello,i am alex")
hello()   #调用函数
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)

也可以带参数:

1.位置参数:

1 def func(name):   #形参
2         pring name
3 func('alex')    #实参

2.默认参数(指如果没有传实参过去那么就取形参时给的值)

def func(name,age = 18):
        print(("name is %s,age is %d)%(name,age)

#指定参数
func('alex',23)
#使用默认参数
func('alex')

3.关键参数

注意:关键参数必须放在位置参数后面,否则会出错

1 def test(x,y,z):
2         print(x)
3         print(y)
4         print(z)
5 test(y=2,x=1,3)

4.动态参数(*args,**args)

def func(*args):
        print(args)
#执行方式一:
func(11,22,33,44) #args=[11,22,33,44]
#执行方式二: func(*[11,22,33,44]) #args=[11,22,33,44]
#总结:*args接收的是n个位置参数,转换成元组
1 def func(**args):
2         print(args)
3 func(name = 'alex',age = '20')   #传入key值和键值   args={'name':alex,'age':20}
4 
5 li = {name: 'alex',age:20,gender:'female'}
6 func(**li)  #传入一个字典   args={name: 'alex',age:20,gender:'female'}

*args接收n个关键字参数,并把关键字参数转换成字典

 输出当前时间:time.strftime()

1 #输出现在的时间
2 import time
3 def logger():
4      time_format = "%Y-%m-%d %X"
5      time_current = time.strftime(time_format)
6      with open('a.txt','a') as f:
7             f.write('%s end action
'%time_current)
8 logger()

 全局变量和局部变量(函数体内定义的变量,只作用于函数体内)

  备注:字符串,单独整数不可在局部里面进行修改;字典,列表,集合,类等都可以在函数里面通过局部变量可以修改值

1 name = "Alex"   #全局变量
2 
3 def change_name(name):
4     print("before:",name)  #name=alex
5     name ="Alex,金角大王"
6     print("after:",name)  #name="Alex,金角大王"
7 change_name(name)
8 print("外面的变量:",name)  #name="Alex"
name = ["alex","jack","rain"]
def change_name():
    name[0]="金角大王"
    print("inside:",name)   #name =["金角大王","jack","rain"]
change_name()
print("外面的name:",name)    #name =["金角大王","jack","rain"]

递归(在函数内部调用自己)

1 def calc(n):
2     print(n)
3     if int(n/2)==0:
4         return n
5     return calc(int(n/2))
6 calc(10)

#输出:
10
5
2
1

递归特性:

1.要有明确的结束条件

2.递归次数不断减少

3.递归层次过多,会导致栈溢出

原文地址:https://www.cnblogs.com/gqq-0504/p/6155678.html