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.
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.
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