Python脚本通过正则表达式处理文本文件:1.如何找出以z为结尾的所有单词(去重) 2. 如何找出4个字母单词及其频率

1.如何找出以z为结尾的所有单词

from mpmath.function_docs import re
import re  # 引入正则表达式模块def main():# 以uft-8模式读取 metadata.txt文件with open('metadata.txt',encoding='utf-8') as f:passage = f.read()# 通过正则表达式记录所有以z为结尾的单词word = re.findall(r'\b\w*z\b', passage)# 去重words = set(word)print(words)
if __name__ == '__main__':main()

2. 如何找出4个字母单词及其频率

from mpmath.function_docs import re
import re  # 引入正则表达式模块def main():with open('metadata.txt',encoding='utf-8') as f:passage = f.read()# 通过正则表达式记录所有4字单词word = re.findall(r'[:alpha:]{4}', passage)# 去重words = set(word)# 记频count_list = list()for i in words:count_list.append((i, word.count(i)))print(count_list)
if __name__ == '__main__':main()

 

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部