python第七次笔记2018.4.14

#申明一个函数,第一个参数是整型,第二个参数是list类型
# l 有一个默认值,默认值为[]空列表
def f(x,l=[]):
for i in range(x):
l.append(i*i)
print(l)


# f(2) = f(2,l=[])

f(2)
#输出 [0,1]

f(3,[3,2,1])
#结果:[3,2,1,0,1,4]

f(3)
#结果:[0,1,4]

如果在同一文件下执行,会有一点影响,f(3)会记住f(2)内存,输出[0,1,,0,1,4]
或者可以在f(3)更改为f(3,l=[]),把l重新定义为空list

'''
函数的关键字
def 定义函数
return 返回值
pass 滤过
exit(1) 直接退出
'''

def add1(x,y):
print(x+y)

def add2(x,y):
return x+y

##差别
add1(1,2)
result = add2(1,2)
print(result)

def hello():
pass
print("hello")
#pass直接滤过,不输出hello
def hello():
exit(1)
print("hello")
#exit(1)直接退出

'''
函数的参数
*args tuple参数,对应赋值
**kwargs dict参数,对应赋值

fun(*args,**kwargs)
fun(1,2,3,4,5,a=10,b=40)
'''

def test(m,*args,**kwargs):
print("m = {0}".format(m))
print("args = {0}".format(args))
print("kwargs = {0}".format(kwargs))

test(10,args=(1,11,12))
#test(m=10,1,2,3,a=1,b=2)

匿名函数的定义:
顾名思义就是没有名字的函数,
lambda 函数是一种快速定义单行的最小函数,可以用在任何需要函数的地方

def fun(x,y)
return x*y

lambda版本:
r = lambda x,y:x*y
ss求绝对值(abs)
'''
匿名函数
def add(x,y):
return x+y

add = lambda x,y:x+y
'''

def add(x,y):
return x+y

add = lambda x,y:x+y

'''
高阶函数 装逼函数
都是可以通过代码逻辑实现的
但是你写到函数的复杂程序,或者算法不一定有人家内置的好
'''

def f(x):
return x*x

for i in map(f,[1,2,3,4]):
print(i)


def f(x):
return x*x

print(list((map(f,[1,2,3,4]))))



def testMap(fun,iter):
l = list()
for i in iter:
l.append(fun(i))
return l


print(testMap(f,[1,2,3,4]))


#reduce

#filter(lambda x:x%2 == 1,[1,2,3,4,5])
#能符合lambda x:x%2 的留下,不符合的去掉

# def add(x,y):
# return x+y

# reduce(f,[1,2,3,4,5])
# 1,2 = 3
# 3, 3 = 6
# 6,4 = 14
# 10,5 = 15

唯一用得比较多的,就是sorted函数


# sorted(iterable,key,reverse)
# iterable 一个可迭代的对象
# key 对什么进行排序
# reverse bool类型,如果为true为反序,如果为false为正序
# 返回值是一个list
print(sorted([1,4,342,3,45,76,435,34],reverse=True))


m = dict(a=1,c=10,b=20,d=15)
print(m)
print(sorted(m.items(),key=lambda x:x[1],reverse=False))
#print(sorted(m.items(),key = lambda d:d[1],reverse = True))

#字典有三种初始化的方法
#第一种 dict(a=1,b=2 )
#第二种 {"a":1,"b":2}
#第三种 dict([("a",1),("b",2)])

m = dict(a=1,c=10,b=20,d=15)
print(m)
print(dict(sorted(m.items(),key=lambda x:x[1],reverse=False)))



'''
列表生成式
[exp for val in collection if condition]
生成器
'''


def jgg():
number = list()
for i in range(1,10):
number.append(i)
for A in [x for x in range(1,10)]:
for B in [x for x in range(1,10) if x != A]:
for C in [x for x in range (1, 10) if x != A and x != B]:
for D in [x for x in range (1, 10) if x != A and x != B and x != C]:
for E in [x for x in range (1, 10) if x != A and x != B and x != C and x != D]:
for F in [x for x in range (1, 10) if x != A and x != B and x != C and x != D and x != E]:
for G in [x for x in range (1, 10) if x != A and x != B and x != C and x != D and x != E and x != F]:
for H in [x for x in range (1, 10) if x != A and x != B and x != C and x != D and x != E and x != F and x != G]:
for I in [x for x in range (1, 10) if x != A and x != B and x != C and x != D and x != E and x != F and x != G and x != H]:
if (A+B+C == D+E+F == G+H+I ==15):
print("A = {0} B = {1} C = {2} D = {3} E = {4} F = {5} G = {6} H = {7} I = {8}".format(A,B,C,D,E,F,G,H,I))
jgg()



a1 = (x for x in range(1,10) if x%2==0)
print(a1)
#python2 a1.next()

print(next(a1)) #python3是直接调用next方法
print("##"*10)
for i in a1:
print(i)


def test():
a= 1
for i in range(1,10):
yield i
a += 1
# return i
#returun 和yield的区别
#yield 可以理解成return,但是比return多一些角色
# yield 每次


m = test()
print(m)


def px(item):
pass
result = ""
with codecs.open("passwd","r") as f:
result = sorted(f.readlines(),key=lambda item: int(item.split(":")[2]))

with codecs.open("sortPasswd","w") as f:
f.writelines(result)

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页