第三章
1.集合的操作; 2 .文件操作;
3.字符编码与解码; 4.函数基本语法、特性
5.参数与局部变量 6.返回值
7.递归 8.匿名函数
9.函数式编程 10.高阶函数
♣集合
集合是一个无序的、不重复的数据组合。
集合的用途:
1.去重,将列表变成集合后,会自动去重
2.集合间的关系,例如:集合的交集、并集,子集关系,对于数据的存储、操作有一定的优势
去重:

1 # Author:chen 2 #_-_coding:utf-8_-_ 3 set_1={1,2,3,4,5,1,2,3,4,5} 4 set_2={1,2,3,4,5,6,7,8,9,10} 5 print(set_1) 6 print(set_2) 7 '''结果是: 8 {1, 2, 3, 4, 5} 9 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 10 '''
集合之间的关系:

1 #Author;chen 2 #_-_coding:utf-8_-_ 3 set_1={1,2,3,4,5,1,2,3,4,5} 4 set_2={1,2,3,4,5,6,7,8,9,10} 5 #判断连个集合之间的关系 6 set_3=set_1.union(set_2)#set_1和set_2的并集,此时,set_1和set_2的位置随意,不会影响结果 7 print(set_3) 8 set_3=set_1.intersection(set_2)#set_1和set_2D的交集,此时,set_1和set_2的位置随意,不会影响结果 9 print(set_3) 10 set_3=set_1.issubset(set_2)#set_2是set_1的子集么,是返回True,否返回False,set_1和set_2的位置影响结果 11 print(set_3) 12 set_3=set_2.issuperset(set_1)#set_2是set_1的父集么,是返回True,否返回False,set_1和set_2的位置影响结果 13 print(set_3) 14 set_3=set_1.isdisjoint(set_2)#判断两个集合是否存在交集,如果不存在则返回True,存在则返回False 15 print(set_3) 16 ''' 17 set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 18 set([1, 2, 3, 4, 5]) 19 True 20 True 21 False 22 '''
集合的关系(符号表示)

1 #Author;chen 2 #_-_coding:utf-8_-_ 3 set_1={1,2,3,4,5,1,2,3,4,5,6,11} 4 set_2={1,2,3,4,5,6,7,8,9,10} 5 6 set_3=set_1 & set_2#交集 7 print(set_3) 8 set_3=set_1 | set_2#并集 9 print(set_3) 10 set_3 =set_1 ^ set_2#set_2在set_1中不存在的值,顺序影响结果 11 print(set_3) 12 set_3=set_1-set_2#差集,在set_1中,但不在set_2中 13 print(set_3) 14 ''' 15 set([1, 2, 3, 4, 5, 6]) 16 set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) 17 set([7, 8, 9, 10, 11]) 18 set([11]) 19 '''
♣文件的操作
文件:

You know the bed feels warmer 你也知道蜷缩在床上会感觉更温暖 Sleeping here alone 哪怕只是独自躺着 You know I dream in colour 你也知道我梦游在色彩当中 And do the things I want 做着我想做的事 You think you got the best of me 你以为你已经从我这里得到一切 Think you had the last laugh 以为是你笑到最后 Bet you think that everything good is gone 打赌你认为美好的事物早已离去 Think you left me broken down 认为你让我伤透了心 Think that I'd come running back 认为我还会回来(找你) Baby you don't know me cause you're devil 但是,亲爱的你不懂我,因为你错得太离谱 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更高 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 What doesn't kill you makes a fire 任何不会致你于死的都会让你变成一个战斗者 Put that thing on ligher 把那些东西看的更轻 Doesn't mean I'm over cause you'r gone 即使在你离去我也不会灭亡 What doesn't kill you makes you stronger stronger 任何不会致你于死的都会让你变得更强,比以往更强 Just me myself and I 那仅仅是我,我自己和最真实的我 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更高 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 You heard that I was starting over with someone 你得知了我开始了一段 new 新的感情 But told you I was moving on over you 他们告诉你我放下了你 You didn't think that I'd come back 你曾以为我不会回来 I'd come back swinging 但是我却带着放任回来了 You try to break me but you see 你试图伤害我,但是你知道 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 What doesn't kill you makes a fire 任何不会致你于死的都会让你变成一个战斗者 Put that thing on lighter 把那些东西看的更轻 Doesn't mean I'm over cause you'r gone 即使在你离去我也不会灭亡 What doesn't kill you makes you stronger stronger 任何不会致你于死的都会让你变得更强,比以往更强 Just me myself and I 那仅仅是我,我自己和最真实的我 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更高 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 Thanks to you I got a new thing started 多亏你,让我找到了新的开始 Thanks to you I'm not a broken hearted 多亏你,你我不再伤心 Thanks to you I'm finally thinking bout me 多亏你,让我最后为自己着想 You know in the end the day to left was just my 你知道我离开只是我的 beginning 新的开始 In the end 在那天的结束时 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更高 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 What doesn't kill you makes a fire 任何不会致你于死的都会让你变成一个战斗者 Put that thing on lighter 把那些东西看的更轻 Doesn't mean I'm over cause you'r gone 即使在你离去我也不会灭亡 What doesn't kill you makes you stronger stronger 任何不会致你于死的都会让你变得更强,比以往更强 Just me myself and I 那仅仅是我,我自己和最真实的我 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更高 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 What doesn't kill you makes you stronger stronger 任何不会致你于死的都会让你变得更强,比以往更强 Just me myself and I 那仅仅是我,我自己和最真实的我 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更高 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 I'm alone 我还是孤单一人
*打开、读取文件、关闭
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import time,sys,os 4 f=open('file_file','r')#获取文件的句柄,赋值给变量f,r表示以只读的模式打开文件 5 for line in f: 6 print(line.strip(' '))#strip()是去掉换行,打印文件的全部内容 7 f.close()#操作完文件一定要关闭文件
*替换文件中的字符串:
1 #! /user/bin/env python 2 #_-_coding:utf-8_-_ 3 import time,sys,os 4 f=open('file_file','r')#获取文件的句柄,赋值给变量f,r表示以只读的模式打开文件 5 for line in f: 6 #print(line.strip(' '))#strip()是去掉换行,打印文件的全部内容 7 if '你也知道蜷缩在床上会感觉更温暖' in line:#判断字符串是否包含在文件内容中 8 line=line.replace('你也知道蜷缩在床上会感觉更温暖',"我也知道蜷缩在床上会感觉更温暖")#替换 9 print(line.strip(' ')) 10 f.close()#操作完文件一定要关闭文件
♣字符编码与转码
在python中,使用unicode类型作为编码的基础类型。即
decode encode
str ---------> unicode --------->str

1 #! /user/bin/env python 2 #coding:utf-8_-_ 3 import time,sys,os 4 print(sys.getdefaultencoding())#查看默认编码格式,是python版本的编码格式,2+默认的是ascii 5 str="陈学涛" 6 print(str.decode("utf-8").encode('gbk'))#将utf-8解码成Unicode,再编码成gbk 7 print(str.decode("utf-8").encode('gbk').decode("gbk"))#将utf-8解码成Unicode,再编码成gbk,再解码成Unicode 8 ''' 9 ascii 10 ��ѧ�� 11 陈学涛 12 '''
♣函数基本语法、特性
函数以def命名,函数名和参数列表,还有函数体
调用函数:函数名即调用函数,执行函数体
不带参数的函数:
1 #! /user/bin/env python 2 #coding:utf-8_-_ 3 4 def count(): 5 print(1+2) 6 count()#函数的调用 7 ''' 8 3 9 '''
函数的特性是:1.减少代码重复 2.可扩展性 3.易于维护
♣参数与局部变量
带参数的函数:
1 #! /user/bin/env python 2 #coding:utf-8_-_ 3 4 def count(x,y):#带参数的函数,x,y是形参 5 print(x+y) 6 count(1,2)#函数的调用,1和2是实参 7 ''' 8 3 9 '''
形参变量只有在被调用时才分配内存单元,在调用结束时,即刻释放所分配的内存单元。因此,形参只在函数内部有效。函数调用结束返回主调用函数后则不能再使用该形参变量
实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,它们都必须有确定的值,以便把这些值传送给形参。因此应预先用赋值,输入等办法使参数获得确定值
局部变量是定义在函数体内部的变量,只在函数体内起作用,也就是说这个变量的作用域是这个函数体,在其作用域外部不可调用
全局变量作用于整个程序,任何一个函数可以使用
来来来,几种参数的使用:
1.默认参数

1 #! /user/bin/env python 2 #coding:utf-8_-_ 3 4 def class_info(name,age,country,course): 5 print('********档案*******') 6 print(name) 7 print(age) 8 print(country) 9 print(course) 10 class_info('li',15,'CN','英语') 11 class_info('ti',15,'CN','语文') 12 class_info('lai',15,'CN','数学') 13 class_info('ke',15,'CN','英语') 14 class_info('chen',15,'CN','美术') 15 class_info('ma',15,'CN','英语') 16 17 #这样CN都是重复的在传参,那不如默认这个参数,看下面 18 19 def class_info2(name,age,course,country='CN'):#默认参数放在最后 20 print('********档案*******') 21 print(name) 22 print(age) 23 print(country) 24 print(course) 25 class_info2('li',15,'英语') 26 class_info2('ti',15,'语文') 27 class_info2('lai',15,'数学') 28 class_info2('ke',15,'英语') 29 class_info2('chen',15,'美术')#以上默认参数country都是CN 30 class_info2('ma',15,'英语',country="USA")#默认参数可以改变,注意位置的对应
2.关键字参数、位置参数

1 #! /user/bin/env python 2 #coding:utf-8_-_ 3 4 def class_info(name,age,country,course): 5 print('********档案*******') 6 print(name) 7 print(age) 8 print(country) 9 print(course) 10 class_info('li',15,'CN',course='英语')#关键字参数和位置参数同时存在,关键字参数放在后边 11 class_info(name='ti',age=15,country='CN',course='语文')#除非都写关键字 12 class_info('lai',15,'CN','数学')#这是默认的位置关键字 13 class_info('ke',15,'CN','英语') 14 class_info('chen',15,'CN','美术') 15 class_info('ma',15,'CN','英语')
3.非固定参数

1 def stu_register(name, age, *args): # *args 会把多传入的参数变成一个元组形式 2 print(name, age, args) 3 4 5 stu_register("chen", 22) 6 # 输出 7 # Alex 22 () #后面这个()就是args,只是因为没传值,所以为空 8 9 stu_register("Tony", 32, "CN", "Python") 10 # 输出 11 # Jack 32 ('CN', 'Python')

1 def stu_register(name, age, *args, **kwargs): # *kwargs 会把多传入的参数变成一个dict形式 2 print(name, age, args, kwargs) 3 4 5 stu_register("bernie", 20) 6 # 输出 7 # bernie 20 () {}#后面这个{}就是kwargs,只是因为没传值,所以为空 8 9 stu_register("Tony", 30, "CN", "Python", sex="Male", province="ShanDong") 10 # 输出 11 # Tony 30 ('CN', 'Python') {'province': 'ShanDong', 'sex': 'Male'}
♣返回值
返回值主要是获取函数的执行结果:
return语句的两个作用
- 函数在执行过程中只要遇到return语句,就会停止执行并返回结果,so 也可以理解为 return 语句代表着函数的结束
- 如果未在函数中指定return,那这个函数的返回值为None
♣递归
在函数内部,可以调用其他函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。

def calc(n): print(n) if int(n/2) ==0: return n return calc(int(n/2)) calc(10) ''' 10 5 2 1 '''
递归特性:
1. 必须有一个明确的结束条件
2. 每次进入更深一层递归时,问题规模相比上次递归都应有所减少
3. 递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出)
♣匿名函数
def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n:n**n print(calc(10))
♣函数式编程
函数式编程就是一种抽象程度很高的编程范式,纯粹的函数式编程语言编写的函数没有变量,因此,任意一个函数,只要输入是确定的,输出就是确定的,这 种纯函数我们称之为没有副作用。而允许使用变量的程序设计语言,由于函数内部的变量状态不确定,同样的输入,可能得到不同的输出,因此,这种函数是有副作 用的。
函数式编程的一个特点就是,允许把函数本身作为参数传入另一个函数,还允许返回一个函数!
Python对函数式编程提供部分支持。由于Python允许使用变量,因此,Python不是纯函数式编程语言。
一、定义
简单说,"函数式编程"是一种"编程范式"(programming paradigm),也就是如何编写程序的方法论
♣高阶函数
变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数。
1 def add(x,y,f): 2 return f(x) + f(y) 3 4 5 res = add(3,-6,abs) 6 print(res)
♣sed

You know the bed feels warmer 你也知道蜷缩在床上会感觉更温暖 Sleeping here alone 哪怕只是独自躺着 You know I dream in colour 你也知道我梦游在色彩当中 And do the things I want 做着我想做的事 You think you got the best of me 你以为你已经从我这里得到一切 Think you had the last laugh 以为是你笑到最后 Bet you think that everything good is gone 打赌你认为美好的事物早已离去 Think you left me broken down 认为你让我伤透了心 Think that I'd come running back 认为我还会回来(找你) Baby you don't know me cause you're devil 但是,亲爱的你不懂我,因为你错得太离谱 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更高 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 What doesn't kill you makes a fire 任何不会致你于死的都会让你变成一个战斗者 Put that thing on ligher 把那些东西看的更轻 Doesn't mean I'm over cause you'r gone 即使在你离去我也不会灭亡 What doesn't kill you makes you stronger stronger 任何不会致你于死的都会让你变得更强,比以往更强 Just me myself and I 那仅仅是我,我自己和最真实的我 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更高 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 You heard that I was starting over with someone 你得知了我开始了一段 new 新的感情 But told you I was moving on over you 他们告诉你我放下了你 You didn't think that I'd come back 你曾以为我不会回来 I'd come back swinging 但是我却带着放任回来了 You try to break me but you see 你试图伤害我,但是你知道 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 What doesn't kill you makes a fire 任何不会致你于死的都会让你变成一个战斗者 Put that thing on lighter 把那些东西看的更轻 Doesn't mean I'm over cause you'r gone 即使在你离去我也不会灭亡 What doesn't kill you makes you stronger stronger 任何不会致你于死的都会让你变得更强,比以往更强 Just me myself and I 那仅仅是我,我自己和最真实的我 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更高 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 Thanks to you I got a new thing started 多亏你,让我找到了新的开始 Thanks to you I'm not a broken hearted 多亏你,你我不再伤心 Thanks to you I'm finally thinking bout me 多亏你,让我最后为自己着想 You know in the end the day to left was just my 你知道我离开只是我的 beginning 新的开始 In the end 在那天的结束时 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更高 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 What doesn't kill you makes a fire 任何不会致你于死的都会让你变成一个战斗者 Put that thing on lighter 把那些东西看的更轻 Doesn't mean I'm over cause you'r gone 即使在你离去我也不会灭亡 What doesn't kill you makes you stronger stronger 任何不会致你于死的都会让你变得更强,比以往更强 Just me myself and I 那仅仅是我,我自己和最真实的我 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更高 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 What doesn't kill you makes you stronger stronger 任何不会致你于死的都会让你变得更强,比以往更强 Just me myself and I 那仅仅是我,我自己和最真实的我 What doesn't kill you makes you stronger 任何不会致你于死的都会让你变得更强 Stand a little taller 比以往站的更高 Doesn't mean I'm lonely when I'm alone 即使是我一人独处也不代表我内心孤独 I'm alone 我还是孤单一人
1 #!/usr/bin/env python 2 #_-_coding:utf-8_-_ 3 #Author:Berniechen 4 import sys 5 with open("sed.txt","r") as f,open("sed_bak.txt","w") as f_new: 6 check = input("要替换的内容>>>") 7 replace = input("要替换成>>>") 8 for line in f: 9 if check in line: 10 line = line.replace(check,replace) 11 f_new.write(line)
♣haproxy配置

global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info defaults log global mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms option dontlognull listen stats :8888 stats enable stats uri /admin stats auth admin:1234 frontend oldboy.org bind 0.0.0.0:80 option httplog option httpclose option forwardfor log global acl www hdr_reg(host) -i www.oldboy.org use_backend www.oldboy.org if www backend www.oldboy.org server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000

1 #Authorchen 2 #_-_coding:utf-8_-_ 3 import sys,os,time,json 4 userchoice =input("请选择:[33[91mcheck33[0m][33[91madd33[0m][33[91mdelete33[0m][33[91mq33[0m]:") 5 sum_file = 0 6 count=0 7 def check(backend): 8 flag = False 9 fetch_list = [] 10 with open('haproxy.txt') as hap: 11 for line in hap: 12 if line.strip() == "backend %s" % (backend): 13 flag = True 14 continue 15 if line.strip().startswith('backend'): 16 flag = False 17 if flag and line.strip(): 18 fetch_list.append(line.strip()) 19 print(fetch_list) 20 exit() 21 hap.close() 22 def add(backend_info): 23 backend_t = backend_info.get("backend") 24 context_t = "backend %s" % (backend_t) 25 record_t = backend_info["record"] 26 context_r = "server %s %s weight %s maxconn %s" % ( 27 record_t["server"], record_t["server"], record_t["weight"], record_t["maxconn"]) 28 check_list = check(backend_t) 29 if check_list: 30 flag = False 31 has_write = False 32 with open('haproxy.txt') as read_add, open('haproxy_bf.txt', 'w') as write_add: 33 for line in read_add: 34 if line.strip() == context_t: 35 write_add.write(" " + line) 36 flag = True 37 continue 38 if flag and line.startswith('backend'): 39 flag = False 40 if flag: 41 for new_line in check_list: 42 if not has_write: 43 temp = "%s%s" % (" " * 8, new_line) 44 write_add.write(temp) 45 has_write = True 46 else: 47 write_add.write(line) 48 read_add.close() 49 write_add.close() 50 51 else: 52 with open('haproxy.txt') as read_obj, open('haproxy_bf.txt', 'w') as write_obj: 53 for line in read_obj: 54 write_obj.write(line) 55 write_obj.write(" " + context_t + " ") 56 temp = " " * 8 + context_r + " " 57 write_obj.write(temp) 58 #read_obj.close() 59 #write_add.close() 60 #os.renames('haproxy.txt', 'haproxy_bak.txt') 61 os.rename('haproxy_bf.txt', 'haproxy_bak.txt') 62 #f=open('haproxy.txt','r') 63 while count<3: 64 with open('haproxy.txt','r') as read_obj: 65 if userchoice=='check': 66 user_www=input('查找域名:') 67 for line in read_obj: 68 if user_www in line and len(line.strip())< (len(user_www)+9): 69 #print(line) 70 concent = check(user_www) 71 elif userchoice=='add': 72 print ('example:>>>{"backend":"ttt.oldboy.org","record":{"server":"100.1.7.9","weight":"20","maxconn":"3000"}}') 73 user_add=input('添加>>>') 74 data_add = json.loads(user_add) 75 add(data_add) 76 exit() 77 elif userchoice=='update': 78 print('该功能优化中....') 79 elif userchoice=='delete': 80 print('该功能优化中....') 81 exit() 82 elif userchoice=='q': 83 exit() 84 else: 85 print('输入错误,请重新输入:') 86 userchoice = input("请选择:[33[91mcheck33[0m][33[91madd33[0m][33[91mdelete33[0m][33[91mq33[0m]:")