Python Forum
Using lambdas and map() to parse substrings in a single line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using lambdas and map() to parse substrings in a single line
#6
To talk a little about Functional programming which these lay under.
So Python has functional features(map,lambda,filter,itertools stuff),but Python is not a functional programming language.

So often there can be discussion what can be most Pytoinc and most effective,there can functional approach not the most popular in Python.
To use example as in Thread as example.
def addition(n):
   ''' Return double of n '''
   return n + n

 # We double all numbers using map()
numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))

# Using list comprehension
result = [addition(i) for i in numbers]
print(result)
Output:
[2, 4, 6, 8] [2, 4, 6, 8]
# Square all numbers using map() and lambda
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 20)
result = map(lambda x: x * x, numbers)
print(list(result))

# Using list comprehension
result = [x * x for x in numbers]
print(result)
Output:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 400] [1, 4, 9, 16, 25, 36, 49, 64, 81, 400]
So in many cases can using list comprehension can be look at a better way to solve this in Python.
In these simple example both sole the task,so both way here are okay,but one may be preferred if look deeper at it.

If you look at this Thread so did a trow out a map example,
but as you see in next post so did @micseydel think more about it and come up with a better solution.

If want to read more about this look at:
Functional Programming HOWTO
Functional Programming in Python
Reply


Messages In This Thread
RE: Using lambdas and map() to parse substrings in a single line - by snippsat - Sep-20-2020, 10:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Substitue multiple substrings in one command Pavel_47 0 838 Jul-18-2022, 01:24 PM
Last Post: Pavel_47
  Presenting multiline data into single line aaronbuhu 1 1,805 Aug-05-2021, 10:57 AM
Last Post: jamesaarr
  Find string between two substrings, in a stream of data xbit 1 2,154 May-09-2021, 03:32 PM
Last Post: bowlofred
  beginner text formatting single line to column jafrost 4 3,217 Apr-28-2021, 07:03 PM
Last Post: jafrost
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,362 Apr-11-2021, 11:03 PM
Last Post: lastyle
  Print characters in a single line rather than one at a time hhydration 1 2,033 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  Python convert multi line into single line formatted string karthidec 2 9,432 Dec-23-2019, 12:46 PM
Last Post: karthidec
  command line input (arg parse) and data exchange Simba 7 4,330 Dec-06-2019, 11:58 PM
Last Post: Simba
  Print string in a single line RavCOder 8 4,181 Nov-08-2019, 09:45 AM
Last Post: perfringo
  Python-for loop print into single line dragan979 4 7,071 Nov-23-2018, 01:01 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020