python note: map -do the same for each element of the list

Motivation

In Previous Post, when I read a text file separated by line breaks and made a list, each element contained a line feed symbol. I wanted to delete it.

Process each element of the list with the map function

I referred to this blog article. By making it a map (process, list object), you can apply the process to each element. I'm about to turn the for statement.

An improved version of the script on a previous post with a motivational link.

contain_or_not.py


import sys

r0 = open('vocab.txt','r') #Open file in read mode
vocab  = r0.readlines()    #vocab.Words are included in txt separated by line breaks
r0.close()

argvs = sys.argv
words = argvs[1:] #argvs[0]Contains the file name at runtime

def remove(s):
  return s.replace('\n','') #replace('Before replacement','After replacement')Replace the corresponding string

new_vocab = list(map(remove, vocab)) #Delete the newline character of each element of vocal with the map function

for word in words:
  if word in new_vocab:
    print(word + ' is in vocab at line %d.' %(new_vocab.index(word)+1))
  else:
    print(word + ' is not in vocab.')

By the way, I also output the line of the list where the word I want to check is.

What is an iterator?

When I used the map function without thinking at first, new_vocab became a map object, so I used the list function to list it again. The map object seems to be called an iterator. Is it an object that has the function of performing the same processing for each element of the list instead of writing a for statement? I still have little understanding.

Recommended Posts

python note: map -do the same for each element of the list
python memo: enumerate () -get index and element of list at the same time and turn for statement
How to count the number of occurrences of each element in the list in Python with weight
Check the operation of Python for .NET in each environment
[Note] List of basic commands for building python / conda environment
Get the index of each element of the confusion matrix in Python
About the basics list of Python basics
Pandas of the beginner, by the beginner, for the beginner [Python]
Python Note: The secret role of commas
Python> sys.path> List of strings indicating the path to search for modules
I measured the speed of list comprehension, for and while with python2.7.
[Python] Master the reading of csv files. List of main options for pandas.read_csv.
[python] Check the elements of the list all, any
[Python] Sort the list of pathlib.Path in natural sort
[Note] Export the html of the site with python.
Match the distribution of each group in Python
Make a copy of the list in Python
A note about the python version of python virtualenv
Image processing? The story of starting Python for
[Note] About the role of underscore "_" in Python
Code for checking the operation of Python Matplotlib
Compare the sum of each element in two lists with the specified value in Python
Python script to get a list of input examples for the AtCoder contest
[For beginners] Simple sample of list operation functions map, reduce, filter for each language
How to get a list of files in the same directory with python
To speed up python, summarize the amount of calculation of collection type (list / tuple / dictionary / set) for each purpose.
List of python modules
Mayungo's Python Learning Note: List of stories and links
How to identify the element with the smallest number of characters in a Python list?
[python] Get the list of classes defined in the module
[Note] The story of setting up the SDK for Python of Azure IoT Hub on Linux
Make a note of the list of basic Pandas usage
the zen of Python
Python program that looks for the same file name
[Python] Get the list of ExifTags names of Pillow library
Python: Get a list of methods for an object
[Maya Python] Crush the contents of the script 2 ~ list Notes
Output the specified table of Oracle database in Python to Excel for each file
Date the name of the decomposed PDF for each page
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Check the processing time and the number of calls for each process in python (cProfile)
How much do you know the basics of Python?
Python Note: The mystery of assigning a variable to a variable
Make sure all the elements in the list are the same in Python
Try to get the function list of Python> os package
[Maya Python] Crush the contents of the script 3 ~ List unknown Plugins
Make a table of multiplication of each element in a spreadsheet (Python)
Get the number of specific elements in a python list
Consideration for Python decorators of the type that passes variables
[Note] Import of a file in the parent directory in Python
Decrypt one line of code in Python lambda, map, list
[Python] The biggest weakness / disadvantage of Google Colaboratory [For beginners]
Google search for the last line of the file in Python
2015-11-26 python> Display the function list of the module> import math> dir (math)
Extract the value closest to a value from a Python list element
[Python] Correct usage of map
Python list, for statement, dictionary
Towards the retirement of Python2
Summary of Python3 list operations
About the ease of Python
3 months note for starting Python