01.05 笔记 - 声明函数
2019-01-05 本文已影响0人
xxxQinli
python中声明函数其实就是声明一个类型是function的变量,函数名就是变量名
a = 10
str1 = 'abc'
list1 = [1, 34, 'abc']
dict1 = {'a': 10, 'b': 100}
func1 = lambda x: x
def func2(): \# 也是一个声明变量的过程,只是省略了=
print('abcd')
\# func1 与 func2都是变量名,声明函数就是在声明变量
函数名 -- 类型是function的变量, 返回<function <lambda> at 0x105b73620>
- 函数名() -- 调用函数并获取函数的返回值
普通变量能做的事情,函数都能做
- 给别的变量赋值
\# 声明了一个列表变量list1
list1 = [1, 2, 3]
\# 声明了一个函数变量func1
def func2():
print('我是一个函数')
return 10
\# 使用列表变量给另外一个变量list2赋值,赋值后list2就可以当成列表来用
list2 = list1
\# 使用函数变量给另外一个变量func2赋值
func2 = func1
\# 赋值后func2就可以当成函数来使用
- 变量作为容器类数据的元素
def func3(x):
print('abc',x)
return 10
list2 = [func3, func3(1), 100]
print(list2) \# [\<function func3 at 0x105c65378\>, 10, 100]
print(list2[0](1)) \# 返回return结果 10
- 变量作为函数的实参
函数1作为函数2的实参 -- 函数2 就是一个高阶函数
a = 10
def func4(n: int):
print(n + n - 1)
func4(a)
def func5(x):
print(x)
x(5)
func5(func4)
func5(lambda x: x\*2)
函数作为参数的应用:sort函数
-
列表.sort()
-
key = 要求是一个带有一个参数,并且返回值是布尔的函数。这儿的参数指向的是列表中元素。确定按照元素的什么值进行排序
list1 = [1, 23, 9, 90]
list1.sort(reverse = True)
print(list1)
all\_student = [
{'name': '张三', 'age': 19, 'score': 90},
{'name': '李四', 'age': 18, 'score': 91},
{'name': '王五', 'age': 21, 'score': 92},
{'name': '张六', 'age': 17, 'score': 93},
]
\#all\_student.sort()
all\_student.sort(key = lambda x:x['age'])
\# 或者
def func(item):
return item['age']
all\_student.sort(key = func) \# 这里func不能加()
print(all\_student)
tuple1 = (
(10, 20),
(10, 10),
(30, 40)
)
sorted(tuple1, key = lambda x: x[1] + x[0])
print(dict(tuple1))
def iterable\_sort(iterable, key = None, reverse = None):
list\_iterable = list(iterable)
if reverse is None:
if key:
\# 有key
for index\_1 in range(len(iterable) - 1):
for index\_2 in range(index\_1 +1 , len(iterable)):
item1 = list\_iterable[index\_1]
item2 = list\_iterable[index\_2]
if key(item1) \> key(item2):
list\_iterable[index\_1], list\_iterable[index\_2] = list\_iterable[index\_2], list\_iterable[index\_1]
return list\_iterable
else:
\#快速排序
for index\_1 in range(len(iterable) - 1):
for index\_2 in range(index\_1 +1 , len(iterable)):
if list\_iterable[index\_1] \> list\_iterable[index\_2]:
list\_iterable[index\_1], list\_iterable[index\_2] = list\_iterable[index\_2], list\_iterable[index\_1]
return list\_iterable
else:
if key:
for index\_1 in range(len(iterable) - 1):
for index\_2 in range(index\_1 +1 , len(iterable)):
item1 = list\_iterable[index\_1]
item2 = list\_iterable[index\_2]
if key(item1) \> key(item2):
list\_iterable[index\_1], list\_iterable[index\_2] = list\_iterable[index\_2], list\_iterable[index\_1]
return list\_iterable[::-1]
else:
\#快速排序
for index\_1 in range(len(iterable) - 1):
for index\_2 in range(index\_1 +1 , len(iterable)):
if list\_iterable[index\_1] \> list\_iterable[index\_2]:
list\_iterable[index\_1], list\_iterable[index\_2] = list\_iterable[index\_2], list\_iterable[index\_1]
return list\_iterable[::-1]
print(iterable\_sort([2,1,3], key = lambda x: x \* -1))
\# 练习:按学生的平均分排序
all\_student = [
{'name': '张三', 'age': 19, 'score': {'c': 78, 'm': 90, 'e': 40}},
{'name': 'stu1', 'age': 30, 'score': {'c': 89, 'm': 60, 'e': 98}},
{'name': 'xiangming', 'age': 12, 'score': {'c': 78, 'm': 67, 'e': 86}},
{'name': 'stu22', 'age': 29, 'score': {'c': 34, 'm': 99, 'e': 50}}
]
def sortByAvrg(iterable):
score = iterable['score']
sum = 0
count = 0
for i in score.values():
sum += i
count += 1
return sum / count
a = sorted(all\_student, key = lambda x: sum(x['score'].values()) / len(x['score']))
b = sorted(all\_student, key = sortByAvrg)
print(a, b)
变量作为函数的返回值
函数1作为函数2的返回值 - 函数2是返回值高阶函数
def operation(char):
if char == '+':
def func1(*nums):
return sum(nums)
return func1
elif char == '-':
def func2(*nums):
if not nums:
return 0
else:
sum1 = nums[0]
for i in nums[1:]:
sum1 -= i
return sum1
return func2
oper = operation('-')
print(oper(1,2,3,4))