Aug-28-2020, 01:59 PM
I’m learning about lambdas and the
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:
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
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:
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:
Also: at the same line, the
Would someone be able to rephrase this in the context of my original script (above)?
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 (content
s), 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)?