[Python] The base application(1): Simple dictionary
Target
Use python to write the function below:
Define a dictionary in the TXT format with a series of definitions for words, such as apple-food, one line for each word. When the python program starts, load the dictionary and prepare to accept the command line input. If the word can be find in the dictionary, output the corresponding word; else, output "unknown".
Q1: Try to use the dict type to do it. But I didn't know how to put the TXT file into the program, and how to save the words imported to the dict?
Q1-1: How to put the TXT file into the program?
Solution Process:
First, I try to use the function "read()" ,and the program is as follows:
file_object = open(r"D:\Python\Python-learning\PYY2-1\dict.txt",'r')
s = f.read()
print(s)
Q1-2: After reading the content, I find it's difficult to save the data into dict as the form of words.
Solution Process:
Then, I replace the function "read()" with "readlines()". In this way, the data can be save as list, and it's convenient to following operation.
The readlines() method reads all the lines of the entire file and saves them in a list variable. Each line acts as an element for subsequent operations. Disadvantages: Reading large files will take up more memory.
The program is as follows:
file_object = open(r"dict.txt",'r')
try:
lines = file_object.readlines()
finally:
file_object.close( )
print(lines)
The results:
['apple\n', 'fruit\n', 'rose\n', 'flower\n', 'time\n', 'money\n']
Q1-3:How to save the words imported to the dict?
The program is as follows:
d = {}
num=0
for word in lines:
if word!='\n':
num+=1
if num%2: #The odd is value.
#myfile.write(word)
d[lines[num-1]] = lines[num]
print(d)
The results:
Obviously, as above, the dictionary is not saved correctly, and it returns "unknown" when I input the word "apple".
After viewing the dict result, I found that the readlines() are read all lines, and each line acts as an element followed the character "\n". so I should remove them.
Q1-3-2:When use the readlines() reads files by lines, how to remove the character "\n"?
The program is as follows:
#remove the character "\n"
for i in range(len(lines)):
lines[i] = lines[i].replace('\n','')
print(lines)
The results:
['apple', 'fruit', 'rose', 'flower', 'time', 'money']
Obviously, we made it.
Now, the result of dict is as follows:
{'apple': 'fruit', 'rose': 'flower', 'time': 'money'}
No "\n" either.
Q2-1: prepare to accept the command line input. If the word can be find in the dictionary, output the corresponding word; else, output "unknown".
I choose to use the function get().
To use Python dictionary, The get() function returns the value of the specified key, If the value is not in the dictionary, returns the default value.
The program is as follows:
word = input('Please input the word:')
print(d.get(word,'unknown'))
The results:
Please input the word:time
money
新增需求: 我们发现,此时程序运行一次后即停止,我们希望新增以下需求:
词典的文件可以随时变,即使你的程序在运行过程中。我需要在你的程序不停地前提下往那个词典里加东西,你的程序要能认新加进去的东西。程序需要是长时间运行,一轮输入输出后它会等待下一次输入。

浙公网安备 33010602011771号