# _*_ encoding:utf-8 _*_
import numpy as np
import re
text = input("请输入单词:")
a = re.findall('(s\w+o)',text)
print(len(a))
m=collections.Counter(text)
print(m)
输出结果:
# _*_ encoding:utf-8 _*_
import numpy as np
import collections
text = input("请输入单词:")
m=collections.Counter(text)
print(m['s'])
print(len([i for i in text.split()]))
输出结果:
# _*_coding: utf-8 _*_
import numpy as np
inport re
class Counter:
def __init__(self, path):
self.mapping = dict()
path = input("请输入单词:")
words = [s.lower() for s in re.findall("\w+", path)]
for word in words:
self.mapping[word] = self.mapping.get(word, 0) + 1
def most_common(self, n):
assert n > 0, "n should be large than 0"
return sorted(self.mapping.items(), key=lambda item: item[1], reverse=True)[:n]
if __name__ == '__main__':
most_common_5 = Counter("path").most_common(5)
for item in most_common_5:
print(item)
还可以用遍历和正则表达式的方式来实现。