01月29日【Python3 基础知识】
5.4 参数匿名函数字典排序 5.5 生成式和生成器 5.6 装饰器的作用
5.4 参数匿名函数字典排序
代码语言:javascript复制# *元组;**字典
def add(*args):
total = 0
for i in args:
total = i
print("total = {0}".format(total))
def sortDictValue(dict1):
print(sorted(dict1.items(), key =lambda d:d[1], reverse=False))
if __name__ == '__main__':
add(1, 2, 3, 4, 5)
s1 = lambda x, y: x y
# def s1(x, y):
# return x y
print(s1(10, 20))
aaa = dict(a = 100, b = 10, c = 50, d = 321)
l = list()
sortDictValue(aaa)
5.5 生成式和生成器
了解return和yield的区别
代码语言:javascript复制a = [x * x for x in range(1, 30) if x % 2 == 0]
print(type(a))
b = (x * x for x in range(1, 30) if x % 2 == 0)
print(type(b))
for i in b:
pass
# print(i)
def test(l):
for i in l:
yield i
print("i = {0}".format(i))
m = test([1, 2, 3])
print(type(m))
for i in m:
print(i)
5.6 装饰器的作用
代码语言:javascript复制def hello():
print("Hello world")
def test():
print("######start######")
hello()
print("######end######")
test()