统计文本中的蒙古文单词出现的次数并从高到低排序,并将排序结果写到文件中python
时间: 2024-03-14 18:43:26 浏览: 25
好的,可以使用Python来实现这个任务。首先,需要读取文本文件中的蒙古文单词,可以使用正则表达式来匹配蒙古文单词,然后使用字典来统计每个单词出现的次数。最后,按照单词出现次数从高到低排序,将排序结果写入文件中。下面是一个简单的Python代码示例:
```python
import re
from collections import defaultdict
# 读取文本文件
with open('text.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 使用正则表达式匹配蒙古文单词
pattern = re.compile('[\u1800-\u18AF]+')
words = pattern.findall(text)
# 统计单词出现次数
word_count = defaultdict(int)
for word in words:
word_count[word] += 1
# 按照单词出现次数从高到低排序
sorted_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
# 将排序结果写入文件
with open('output.txt', 'w', encoding='utf-8') as f:
for word, count in sorted_words:
f.write(f'{word}: {count}\n')
```
其中,`text.txt`是待处理的文本文件,`output.txt`是输出文件,需要根据实际情况修改文件名和路径。运行代码之后,输出文件中将会按照单词出现次数从高到低的顺序列出每个单词及其出现次数。
相关推荐
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)