I am not clear with concepts of filter vs map vs reduce functionsin list data structure.Can anyone explain them with examples.when and where they can be used??
Thanks in advance
2 4779 bvdet 2,851
Expert Mod 2GB
I am not clear with concepts of filter vs map vs reduce functionsin list data structure.Can anyone explain them with examples.when and where they can be used??
Thanks in advance
Here are examples of each: - >>> def imp_to_mm(inches):
-
... return inches * 25.4
-
...
-
>>> inchesList = [1,2,12,24,36]
-
>>> map(imp_to_mm, inchesList)
-
[25.399999999999999, 50.799999999999997, 304.79999999999995, 609.59999999999991, 914.39999999999998]
-
>>> def is_str(item):
-
... if isinstance(item, str):
-
... return True
-
... return False
-
...
-
>>> filter(is_str, ['a', 1, 12.5, [3,4,5], 'BBB', inchesList, 'A string'])
-
['a', 'BBB', 'A string']
-
>>> reduce(lambda x,y: x+y, [1,2,3,4,5,6,7,8,9,10])
-
55
-
>>>
map() applies a function to each element of a list and returns a new list. filter() filters the elements of a list with a filter function (the function should return True or False) and returns a new list consisting of the elements of the argument list that evaluate True. I prefer to use a list comprehension. - >>> [i for i in ['a', 1, 12.5, [3,4,5], 'BBB', inchesList, 'A string'] if isinstance(i, str)]
reduce() collects information from a sequence and returns a single value. It works by applying the argument function to the first two elements of the sequence. The value is then combined with the third element in the sequence and so on.
Here are examples of each: - >>> def imp_to_mm(inches):
-
... return inches * 25.4
-
...
-
>>> inchesList = [1,2,12,24,36]
-
>>> map(imp_to_mm, inchesList)
-
[25.399999999999999, 50.799999999999997, 304.79999999999995, 609.59999999999991, 914.39999999999998]
-
>>> def is_str(item):
-
... if isinstance(item, str):
-
... return True
-
... return False
-
...
-
>>> filter(is_str, ['a', 1, 12.5, [3,4,5], 'BBB', inchesList, 'A string'])
-
['a', 'BBB', 'A string']
-
>>> reduce(lambda x,y: x+y, [1,2,3,4,5,6,7,8,9,10])
-
55
-
>>>
map() applies a function to each element of a list and returns a new list. filter() filters the elements of a list with a filter function (the function should return True or False) and returns a new list consisting of the elements of the argument list that evaluate True. I prefer to use a list comprehension. - >>> [i for i in ['a', 1, 12.5, [3,4,5], 'BBB', inchesList, 'A string'] if isinstance(i, str)]
reduce() collects information from a sequence and returns a single value. It works by applying the argument function to the first two elements of the sequence. The value is then combined with the third element in the sequence and so on.
I know this forum will never disappoint and will clarify every minute doubts without criticizing.
Thank u so much
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Stephen C. Waterbury |
last post by:
This seems like it ought to work, according to the
description of reduce(), but it doesn't. Is this
a bug, or am I missing something?
Python 2.3.2 (#1, Oct 20 2003, 01:04:35)
on linux2
Type...
|
by: Ben |
last post by:
Hi all,
I'm trying to figure out how how complex map, filter and reduce work
based on the following piece of code from
http://www-106.ibm.com/developerworks/linux/library/l-prog.html :
...
|
by: Logan |
last post by:
I read in PEP 279 the following comment by GvR:
"filter and map should die and be subsumed into list
comprehensions, not grow more variants."
Actually, I am using 'filter' and 'map' a lot in...
|
by: Xah Lee |
last post by:
© # -*- coding: utf-8 -*-
© # Python
©
© # the "filter" function can be used to
© # reduce a list such that unwanted
© # elements are removed.
© # example:
©
© def even(n): return n % 2 == 0...
|
by: Tom Anderson |
last post by:
Comrades,
During our current discussion of the fate of functional constructs in
python, someone brought up Guido's bull on the matter:
http://www.artima.com/weblogs/viewpost.jsp?thread=98196
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |