python之路第四篇(基础篇)

时间:2021-01-25 08:24:16

一、冒泡算法实现:

方法一:

li = [13,33,12,80,66,1]

print li
for m in range(4):
num1 = li[m]
num2 = li[m+1]
if num1 > num2:
temp = li[m]
li[m] = li[m+1]
li[m+1] = temp
print li for m in range(3):
num1 = li[m]
num2 = li[m+1]
if num1 > num2:
temp = li[m]
li[m] = li[m+1]
li[m+1] = temp
print li

方法二:

li = [13,33,12,80,66,1]
print li
for n in range(1,len(li)):
for m in range(len(li)-n):
num1 = li[m]
num2 = li[m+1]
if num1 > num2:
temp = li[m]
li[m] = li[m+1]
li[m+1] = temp
print li

二、上周作业:(修改haproxy文件)

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os
import json def fetch(backend):
fetch_list = []
with open('ha') as obj:
flag = False
for line in obj:
# line 每一行,
if line.strip() == "backend %s" % backend:
flag = True
continue
# 判断,如果当前是 backend开头,不再放
if flag and line.strip().startswith('backend'):
break
if flag and line.strip():
fetch_list.append(line.strip())
return fetch_list #result = fetch("buy.oldboy.org")
#print result
def add1(dict_info):
#s = '{"bakend": "www.oldboy.org","record":{"server": "100.1.7.9","weight": 20,"maxconn": 30}}'
backend_title = dict_info.get('backend')
current_title = "backend %s" % backend_title
crrent_record = "server %s %s weight %s maxconn %s" % (dict_info['record']['server'],dict_info['record']['server'],dict_info['record']['weight'],dict_info['record']['maxconn'])
# 获取制定backend下的所有记录
fetch_list = fetch(backend_title)
# backend是否存在
if fetch_list:
#pass # 存在backend,则只需再添加记录
# 1,要插入的记录,存在
# 1,要插入的记录,不存在
if crrent_record in fetch_list:
pass
else:
fetch_list.append(crrent_record)
# fetch_list,处理完的新列表
with open('ha') as read_obj, open('ha.new', 'w') as write_obj:
flag = False
has_write = False
for line in read_obj:
if line.strip() == current_title:
write_obj.write(line)
flag = True
continue
if flag and line.strip().startswith('backend'):
flag = False
if flag:
# 中,把列表所有数据写入
if not has_write:
for new_line in fetch_list:
temp = "%s %s \n" %(" "*8, new_line)
write_obj.write(temp)
has_write = True
else:
# 上,下
write_obj.write(line)
else:
#pass # 不存在backend,添加记录和backend
# current_title,crrent_record
# 直接打开文件,a
with open('ha') as read_obj, open('ha.new', 'w') as write_obj:
for line in read_obj:
write_obj.write(line)
write_obj.write('\n')
write_obj.write(current_title+'\n')
temp = "%s %s \n" %(" "*8, crrent_record)
write_obj.write(temp) #os.rename("ha", 'ha.bak')
#os.rename("ha.new", 'ha') s = '{"backend": "www.oldboy.org","record":{"server": "100.1.7.9","weight": 20,"maxconn": 3000}}'
data_dict = json.loads(s)
add1(data_dict)

上周拾忆:

函数:

三元运算:简化的if else
lambda:简化的函数定义
lambda: 处理简单逻辑,自动return

普通函数实现:

def fun1(arg):
return arg + 1
result = fun1(10)
print result

lamdba 实现:

fun2 = lambda a: a+1
result = fun2(10)
print result

lambda 也可用多参数:

fun2 = lambda a,b: a+b
result = fun2(10,20)
print result

内置函数:

1)map:遍历序列,对序列中每个元素进行操作,最终获取新的序列。

li = [1,2,3,4,5]
def fun1(arg):
return arg + 100
new_list = map(fun1,li)
print new_lis li1 = [1,2,3]
li2 = [2,3,5]
li3 = [1,2,4]
def fun2(a,b,c):
return a + b +c
print map(fun2,li1,li2,li3)

上面的列子,下面一句搞定:

print map(lambda a,b,c:a+b+c,li1,li2,li3)

2)filter: 对于序列中的元素进行筛选,最终获取符合条件的序列

li = [12,3,4,]
print filter(None,li) li = [12,3,'',False,0]
print filter(None,li) li = [12,3,4,3,55]
print filter(lambda a:a>40,li)

3)reduce:对于序列内所有元素进行累计操作

li = [1,2,3,4,5,6]
print reduce(lambda arg1,arg2:arg1*arg2,li) reduce的第一个参数,函数必须要有两个参数
reduce的第二个参数,要循环的序列
reduce的第三个参数,初始值 li = [1,2,3,4,5,6]
print reduce(lambda arg1,arg2:arg1*arg2,li,10000) #10000 是基数

4)map、filter、reduce对比:

map:对所有元素操作
filter:过滤指定指定元素集合
reduce:累积操作

新内容:

yield:

记住上次操作,下次执行时,继续执行

函数内,return,函数执行完毕(生命周期)

yield 类似一个生成器

def fun1():  # 用debug 调试看看执行过程
yield 1
yield 2
yield 3
yield 4
for i in fun1():
print i

自己制作一个类似xrange 的生成器

def myrange2(arg):
seed = 0
while True:
if seed >10:
seed = seed +1
yield seed
for i in myrange(10):
print i

--

def myrange(arg):
seed = 0
while True:
seed = seed +1
if seed > arg:
return
else:
yield seed
for i in myrange(10):
print i

更多链接:http://www.cnblogs.com/wupeiqi/articles/4963027.html