In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use the higher-order functions of Python3". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. High-order function # 1. Variable points to function # calling function and function itself print ("- 10) absolute value is:", abs (- 10) print ("absolute value function itself:", abs) print ("- -") # assigns the result of the function call and the function itself to the variable That is, the variable points to the function abs1 = abs (- 100) print (the absolute value of "- 100) selfAbs = absprint (" abs function itself: ", selfAbs) print ("-- ") # 2. The function name is also a variable # function name: the variable pointing to the function; # can point the abs to-10, that is, abs =-1 to 3. Incoming function: one function can take another function as an argument, which is called a higher-order function; # example: def add (xmemyjournal f): return f (x) + f (y) # calls the add () function print (the value of "add (- 5mam 10memabs) is:", add (- 5mai 10memabs)
# result output:
The absolute value of-10 is: 10
The absolute value function itself:
The absolute value of-100 is: 100
The abs function itself:
The value of add (- 5 mai mei 10 mai abs) is: 15
2.map/reduce# reduce# reduce acts a function on a sequence [x1 ~ x2.] This function must take two arguments # reduce accumulates the result and the next element of the sequence # reduce (f, [x1scorex2dx3recoverx4]) = f (f (x1scorex2), x3), x4) # example: from functools import reducedef add (xrecoery y): return x + ynumsList = [1Med 3LINGONE 5JERE7] print ("the result of reduce acting on numsList is:", reduce (add) NumsList)) print ("- -") # calculate the factorial of 1-10 def mul (XMague y): return x * ynumsList = list (range (1Mague 11)) print ("use reduce function to calculate the factorial of 1-10:", reduce (mul) (numsList) # result output: "the result of reduce acting on numsList is: 25 reduce-use the reduce function to calculate the factorial of 1-10: 3628800" # reduce# reduce acts a function on a sequence [x1jinx2jue.] This function must take two arguments # reduce accumulates the result and the next element of the sequence # reduce (f, [x1scorex2dx3recoverx4]) = f (f (x1scorex2), x3), x4) # example: from functools import reducedef add (xrecoery y): return x + ynumsList = [1Med 3LINGONE 5JERE7] print ("the result of reduce acting on numsList is:", reduce (add) NumsList)) print ("- -") # calculate the factorial of 1-10 def mul (XMague y): return x * ynumsList = list (range (1Mague 11)) print ("use reduce function to calculate the factorial of 1-10:", reduce (mul) NumsList) # result output: "" the result of reduce acting on numsList is: 25 reduce-use the reduce function to calculate the factorial of 1-10: 3628800 "" 3.filterdef is_odd (n): return n% 2 = delete the even number in list. Keep odd numbers numsList = [1 numsList 2, numsList) print ("results after filter:", list (filter (is_odd,numsList)) print ("- -") # find primes # 1 by Ehrlich sieve method. Construct an odd sequence def _ odd_iter (): n = 1 while True: n = n + 2 yield n # 2. Define a filter function def _ not_divisible (n): return lambda x: X% n > filter 3. Define a generator Continue to return the next prime def primes (): yield 2 it = _ odd_iter () # initial sequence while True: n = next (it) # the first number of the return sequence yield n it = filter (_ not_divisible (n), it) # construct a new sequence # print primes within 1000 for n in primes (): if n < 1000: print (n End = "") else: break
# result output:
NumsList content is: [1, 2, 5, 7, 19, 23, 3, 6, 9]
Results after filter: [1, 5, 7, 19, 23, 3, 9]
23 57 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# sorting algorithm # sorted () function can sort list NumsList = [1 list, 2, 4, 3, 9, 6, 8 and 7] print ("original sorted content:", numsList) print ("list content sorted using sorted:" Sorted (numsList)) print ("- -") # sorted () function can accept a key function to implement custom sorting # sort by absolute value numsList2 = [- 10mem2, 3, 4, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 NumsList2) print ("sort by absolute value:", sorted (numsList2,key = abs)) print ("- -") # sorts the list of strings regardless of case nameList = ["Willard", "ChenJD", "ChenBao", "ChenXiaoBao" "hackerLuo"] print ("nameList original content is:", nameList) print ("ignore case sorting:", sorted (nameList,key = str.lower)) print ("reverse sort:", sorted (nameList,key = str.lower,reverse = True))
# result output:
Original list content: [1, 2, 4, 3, 9, 6, 8, 7, 0]
List content sorted using sorted: [0,1,2,3,4,6,7,8,9]
Original list content: [- 10, 2, 3, 4, 6, 5,-9,-8, 7]
Sort by absolute value: [2, 3, 4, 5, 6, 7,-8,-9,-10]
The original content of nameList is: ['Willard',' ChenJD', 'ChenBao',' ChenXiaoBao', 'hackerLuo']
Ignore case sorting: ['ChenBao',' ChenJD', 'ChenXiaoBao',' hackerLuo', 'Willard']
Sort the list in reverse: ['Willard',' hackerLuo', 'ChenXiaoBao',' ChenJD', 'ChenBao']
This is the end of the content of "how to use the higher-order functions of Python3". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.