有一个.txt的文本文件,对齐单词进行统计,并显示单词重复做多的10个单词
思路:
1将文本文件进行逐行处理,并进行空格分割处理
2新建一个字典,使用get方法将单词一次添加到字典中,并用sorted方法进行排序。

这里解释一下最后一句的意思,有些同学可能看不懂。

3统计path单词个数


但是path有很多其他符号,如何提取,只保留path单词呢?
方法1:
创建一个列表,使用replace将特俗符号换成空格,添加到列表中,不是的直接添加到列表中,使用split方法分割出空格

方法二使用正则表达式

方法三使用切片
def make_key(line: str):
line = line.lower()
chars = set("""~!@#$%^&*()_-+=|{}[]:;"'<>?/,.\n\r\t""")
ret = []
start=0
length=len(line)
for i,c in enumerate(line):
if c in chars:
if start==i:#说明连续两个字符是特殊字符,
start+=1
continue#跳过
ret.append(line[start:i])
start=i+1
else:
if start<length:
ret.append(line[start:])
return ret
line="b([a"#出现连续非法字符
#line="""os.path.commomprefix(['/usr/lib' split/replace sub'"""
print(*make_key(line))
完整代码
