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
#3
Thank you, @ndc85430, this helps tremendously.

Based on your feedback, here is the next iteration of my script:

def replace_n(set_st):
   return str.replace(set_st, "\n", "")
          
def tail(filename, nums):
   with open(filename, 'r') as lines:
       result = lines.readlines()[-(nums):]
       result_without_n = list(map(replace_n, result))
       return result_without_n
 
print(tail('Alice.txt', 3))
This achieves the same thing as my original script but with a regular function instead of a lambda. Here is the output:

Quote:['including how to make donations to the Project Gutenberg Literary', 'Archive Foundation, how to help produce our new eBooks, and how to', 'subscribe to our email newsletter to hear about new eBooks.']

By the way, I noticed that when the first function is inserted in between lines 6 and 7, the script still runs and processes the text file just as well.

I still have a few questions about my new script.

At line 7, the first argument inside the map() built-in must be an iterable. Iterables are usually data structures such as strings, lists, tuples, among others. But this script, replace_n() is a function. Is it really possible to iterate over a function even when the return value of the replace_n() function is not a string, list, or tuple? At line 2, the replace() method of the str built-in is called with an empty argument, set_st. How is this possible? Why does Python continue to iterate successfully over the replace_n() function when the return value includes an empty argument? I’d expect Python to cough up Type Error since replace_n() is not an iterable. To phrase my confusion in another way: set_st is initialized at line 1 but when it is referred to at line 2, there is no value being passed in. It’s just empty. Why does the set_st argument work if it is empty? When the replace_n() function is called at line 7, no argument is specified.

Exploring map(), I came across another tutorial on Geeks for Geeks which I’ve been playing with but still don’t understand how Python is able to iterate over a function (or lambda expression):

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))
 
# 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))
Output:

Quote:[2, 4, 6, 8]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 400]
Reply


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

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