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
#1
I’m learning about lambdas and the map() builtin. I’ve got an elegant lambda expression I came across online which takes a list of three subs strings and replaces all instances of “444” with an empty “”. The code accomplishes this without meticulously converting the list to a string, replacing “444” and converting back into a list (which would have been my initial unpythonic first attempt/approach).

Based on the Geeks for Geeks tutorial titled, "Python | Replace substring in list of strings" I’ve modified their script slightly. Here is my modified code with map() and a lambda expression:

content = [
   "It was all very well to say Drink me, but the wise little Alice was not going to do THAT in a hurry. 444",
   "No, I'll look first,' she said, 444",
   "and see whether its marked poison or not for she had read several nice 444"]

 # printing original list  
print(f"\nThe original list :  \n {content}")

result = list(map(lambda set_st: str.replace(set_st, "444", ""), content))
    
# print result:
print(f"\n The list after substring replacement :  \n{result} \n")
Here is the expected output:

Quote:$ python exploring_lambda.py

The original list :
['It was all very well to say Drink me, but the wise little Alice was not going to do THAT in a hurry. 444', "No, I'll look first,' she said, 444", 'and see whether its marked poison or not for she had read several nice 444']

The list after substring replacement :
['It was all very well to say Drink me, but the wise little Alice was not going to do THAT in a hurry. ', "No, I'll look first,' she said, ", 'and see whether its marked poison or not for she had read several nice ']

I am not entirely sure how or why it works so well. I struggle with line 9 in my code sample. I partially understand some of what is going on, which I will now explain to the best of my ability. I need other forum members reading this thread to jump in and correct me or fill in gaps in my explanation. Here goes:

Line 9 declares a variable called result and processes the list of sub strings (contents), by replacing all the instances of ‘444’ with empty ‘’. After all instances of 444 are replaced, Python maps and converts it back to a list.

Is that correct? What might you people add to this?

What is map() doing? The official Python docs describes the map() builtin in this way:

Quote:returns an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.

All of this reads like a foreign language to me. This entire block quote goes over my head. Would someone kindly rephrase this in the context of my code sample above?

I understand that lambda is like a single use function which is typically invoked in a single line, for the sake of elegance and simplicity. Here it is at line 9 again: result = list(map(lambda set_st: str.replace(set_st, "444", ""), content)). I see that there are two instances of the lambda name, set_st. The second instance of set_st is a reference to itself (within itself). So would this be an example of recursion?

Also: at the same line, the str method is passed into the replace() function. How does this work? Again, here is the official Python doc on the subject of str.replace() which apparently isn’t written in plain english either:

Quote: str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

Would someone be able to rephrase this in the context of my original script (above)?
Reply


Messages In This Thread
Using lambdas and map() to parse substrings in a single line - by Drone4four - Aug-28-2020, 01:59 PM

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