本系列博文基于廖雪峰老师的官网Python教程,笔者在大学期间已经阅读过廖老师的Python教程,教程相当不错,官网链接: 廖雪峰官方网站.请需要系统学习Python的小伙伴到廖老师官网学习,笔者的编程环境是Anaconda+Pycharm,Python版本:Python3.
1.高阶函数
print("-10的绝对值为:",abs(-10))
print("绝对值函数本身:",abs)
print("--------------------------------------------------")
abs1 = abs(-100)
print("-100的绝对值为:",abs1)
selfAbs = abs
print("abs函数本身:",selfAbs)
print("--------------------------------------------------")
def add(x,y,f):
return f(x) + f(y)
print("add(-5,-10,abs)的值为:",add(-5,-10,abs))
-10的绝对值为: 10
绝对值函数本身: <built-in function abs>
--------------------------------------------------
-100的绝对值为: 100
abs函数本身: <built-in function abs>
--------------------------------------------------
add(-5,-10,abs)的值为: 15
2.map/reduce
def f(x):
return x * x
numsList = [1,2,3,4,5,6,7,8,9]
result = map(f,numsList)
print("f(x)作用到numsList上结果为:",list(result))
print("-------------------------------------------------------------")
numToList = list(map(str,numsList))
print("把numsList中的数字转为字符串的结果为:",numToList)
"""
f(x)作用到numsList上结果为: [1, 4, 9, 16, 25, 36, 49, 64, 81]
-------------------------------------------------------------
把numsList中的数字转为字符串的结果为: ['1', '2', '3', '4', '5', '6', '7', '8', '9']
"""
from functools import reduce
def add(x,y):
return x + y
numsList = [1,3,5,7,9]
print("reduce作用于numsList的结果为:",reduce(add,numsList))
print("-------------------------------------------------------------")
def mul(x,y):
return x * y
numsList = list(range(1,11))
print("使用reduce函数计算1-10的阶乘:",reduce(mul,numsList))
"""
reduce作用于numsList的结果为: 25
-------------------------------------------------------------
使用reduce函数计算1-10的阶乘: 3628800
"""
3.filter
def is_odd(n):
return n % 2 == 1
numsList = [1,2,5,7,19,23,3,6,9]
print("numsList内容为:",numsList)
print("filter后的结果:",list(filter(is_odd,numsList)))
print("--------------------------------------------------")
def _odd_iter():
n = 1
while True:
n = n + 2
yield n
def _not_divisible(n):
return lambda x: x % n > 0
def primes():
yield 2
it = _odd_iter()
while True:
n = next(it)
yield n
it = filter(_not_divisible(n), it)
for n in primes():
if n < 1000:
print(n,end = " ")
else:
break
numsList内容为: [1, 2, 5, 7, 19, 23, 3, 6, 9]
filter后的结果: [1, 5, 7, 19, 23, 3, 9]
--------------------------------------------------
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997
4.sorted
numsList = [1,2,4,3,9,6,8,7,0]
print("原始的list内容:",numsList)
print("使用sorted排序后list内容:",sorted(numsList))
print("----------------------------------------------------------")
numsList2 = [-10,2,3,4,6,5,-9,-8,7]
print("原始的list内容:",numsList2)
print("按照绝对值排序:",sorted(numsList2,key = abs))
print("----------------------------------------------------------")
nameList = ["Willard","ChenJD","ChenBao","ChenXiaoBao","hackerLuo"]
print("nameList原始内容为:",nameList)
print("忽略大小写排序:",sorted(nameList,key = str.lower))
print("对列表反向排序:",sorted(nameList,key = str.lower,reverse = True))
原始的list内容: [1, 2, 4, 3, 9, 6, 8, 7, 0]
使用sorted排序后list内容: [0, 1, 2, 3, 4, 6, 7, 8, 9]
----------------------------------------------------------
原始的list内容: [-10, 2, 3, 4, 6, 5, -9, -8, 7]
按照绝对值排序: [2, 3, 4, 5, 6, 7, -8, -9, -10]
----------------------------------------------------------
nameList原始内容为: ['Willard', 'ChenJD', 'ChenBao', 'ChenXiaoBao', 'hackerLuo']
忽略大小写排序: ['ChenBao', 'ChenJD', 'ChenXiaoBao', 'hackerLuo', 'Willard']
对列表反向排序: ['Willard', 'hackerLuo', 'ChenXiaoBao', 'ChenJD', 'ChenBao']
|