查找文本文件中重复的汉字
查找文本文件中重复的汉字,找到在所在行号
文本文件格式
大
小
多
少
前
后
左
……
text.py
import os import re list = [] same=0 total=0 index = [] with open("1.txt", "r", encoding="utf-8") as f: for line in f.readlines(): line = line.strip('\n') #去掉列表中每一个元素的换行符 if len(line) ==0: break total +=1 if line in list: print(line,"有多个在第",total,"行") same=same+1 list.append(line) print("相同的有:",same,"个") print("一共有:",total,"个字") import os import re list = [] same=0 total=0 index = [] with open("1.txt", "r", encoding="utf-8") as f: for line in f.readlines(): line = line.strip('\n') #去掉列表中每一个元素的换行符 if len(line) ==0: break total +=1 if line in list: print(line,"有多个在第",total,"行") same=same+1 list.append(line) print("相同的有:",same,"个") print("一共有:",total,"个字")
运行脚本
$python text.py 药 有多个在第 160 行 相同的有: 1 个 一共有: 200 个字