In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "the role of Python built-in functions Map, Reduce and Filter". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the function of Python built-in functions Map, Reduce and Filter".
1. Map
Mapping the specified sequence according to the provided function, the first parameter, function, calls the function function with each element in the parameter sequence, returning an iterator containing the return value of each function function
Map (function, iterable,...)
Function: function
Iterable: one or more sequences
> def square (x): # calculate the square. Return x * * 2... > map (square, [1gime 2jin3, 4je 5]) # calculates the square of each element of the list [1,4,9,16,25] > map (lambda x: X * * 2, [1Jing 2Jing 3Jing 4Jing 5]) # provides two lists using the lambda anonymous function [1,4,9,16,25] # Add list data in the same position > map (lambda x, y: X + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]) [3, 7, 11, 15, 19]
2. Reduce
Accumulate the elements in the parameter sequence, and the function performs the following operations on all the data in a data set (linked list, tuple, etc.): use the function function (with two parameters) passed to reduce to first operate on the first and second elements in the set, and then calculate the result with the third data using the function function, and finally get a result.
Reduce (function, iterable [, initializer])
Function: function with two arguments
Iterable: iterable object
Initializer: optional, initial parameter
> def add (x, y): # the addition of two numbers. Return x + y... > reduce (add, [1JEI 2Jing 3Jing 4JEI 5]) # Compute list and reduce (lambda x, y: XLY, [1Med 2Jing 3Jing 4Jing 5]) # use lambda anonymous function to implement factorial function reduce (lambda x, y: X * y, range (1, num))
3. Filter
Used to filter sequences, filter out elements that do not meet the criteria, and return an iterator object
Filter (function, iterable)
Function: judgment function.
Iterable: iterable object.
# filter out all odd numbers in the list: #! / usr/bin/python3 def is_odd (n): return n% 2 = = 1 tmplist = filter (is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) newlist = list (tmplist) print (newlist) so far, I believe you have a better understanding of "the role of Python built-in functions Map, Reduce and Filter". Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.