python | Clever use of dictionary get method to achieve word frequency statistics and relationship mapping

Python dictionary has many built-in functions and methods, among which the get() method is the most basic and common query method and can be flexibly used in multiple scenarios.
The get() method uses syntax:
dict.get(key[, value]), returns the value of the specified key (key)

parameter describe
key The key to be searched for
value Optional parameter, when the value of the specified key does not exist, return the value (value). The default value of value is None, so when using this method to find the value of the specified key, even if the specified key does not exist, the program will not report an exception, but return None.

According to the get() usage of Python dictionary, it can often be used for word frequency statistics and simple relationship mapping.
1. Implement word frequency statistics
. Word frequency statistics, as the name implies, is to count the number of times a certain word, phrase, etc. appears in a sample. It is often used after segmenting a text.
Example: Count the number of occurrences of each word in an existing list (a_list)

# 词频统计
a_list = ['西瓜', '苹果', '香蕉', '西瓜', '苹果', '梨子', '梨子', '梨子']
a_count = {
    
    }
for a_key in a_list:
    a_count[a_key] = a_count.get(a_key, 0) + 1
print('词频统计结果:\n', a_count)

Insert image description here
Analysis: a_count.get(a_key, 0), matches the corresponding value according to a_key. If the key a_key does not exist, 0 is returned.
Note: To count word frequency and sort, the above code can be modified as

# 词频统计并排序
a_list = ['西瓜', '苹果', '香蕉', '西瓜', '苹果', '梨子', '梨子', '梨子']
a_count = {
    
    }
for a_key in a_list:
    a_count[a_key] = a_count.get(a_key, 0) + 1  # 计数
a_count = sorted(a_count.items(), key=lambda x: x[1], reverse=True)  # 排序,返回列表格式数据
a_count = dict(a_count)  # 转换为字典格式
print('词频统计并排序:\n', a_count)

Insert image description here
For dictionary sorting method, please refer to: https://blog.csdn.net/LHJCSDNYL/article/details/122525942

2. Simple relational mapping
According to the "key-value pair" format of the dictionary, it can be used to process some simple relational mappings.
Example: Implement Chinese-English mapping, input Chinese and get English results

# 实现关系映射
a_zh = ['西瓜', '苹果', '梨子']
b_en = ['watermelon', 'apple', 'pear']
a_dict = dict(zip(a_zh, b_en))
while 1:
    a_key = input('输入中文名:')
    print('英文名为:', a_dict.get(a_key))

Insert image description here
Note: The above relationship mapping is just an example, and can actually be used to make judgments, choices, etc. However, the prerequisite for mapping relationships is to construct a dictionary mapping relationship. For example, in the above example, a Chinese-English comparison dictionary is constructed.
For the construction method of dictionary, please refer to: https://blog.csdn.net/LHJCSDNYL/article/details/122294020

The above are two common usage scenarios of the dictionary get() method.

Follow [One Code] on WeChat to learn more about solutions to python and mysql related problems.
-end-

Guess you like

Origin blog.csdn.net/LHJCSDNYL/article/details/126293039