python的wordcount程序

# 计算给定文章的单词出现个数wordcount
str_word = '''
    Once a circle missed a wedge The circle wanted to be whole so it
    went around looking for its missing piece But because it was incomplete
    and therefore could roll only very slowly it admired the flowers
    along the way It chatted with worms It enjoyed the sunshine It
    found lots of different pieces but none of them fit So it left them
     all by the side of the road and kept on searching Then one day the
     circle found a piece that fit perfectly It was so happy Now it could
     be whole with nothing missing It incorporated the missing piece into
     itself and began to roll Now that it was a perfect circle it could roll
     very fast too fast to notice flowers or talk to the worms When it realized
     how different the world seemed when it rolled so quickly it stopped left its
     found piece by the side of the road and rolled slowly away
    '''

# 思路:将序列中的文字按照空格切分,然后将结果放到一个字典中

# 引入对象比较模块operator
import operator

dict_word = {}  # 创建字典

# 对序列中的英文做小写处理,然后空格切分,split()默认就是以空格切分,切分完返回一个新的列表
clean_word = str_word.lower().split(' ')   

for word in clean_word :    # 遍历切分好的列表clean_word
    if word not in dict_word:    # 判断当遍历的单词不在字典中时,我们给这个词计做1
        dict_word[word] = 1
    else:                        # 当字典中存在这个单词时,我们给它的value值+1
        dict_word[word] += 1

it = dict_word.items()        # 调用字典方法items()将字典中的{'k':v,'k2',v2...}格式转化为[(k,v),(k2,v2)...]

# 调用sorted()函数排序,传入it,itemgetter(1)的意思是取每个tuple中第二个元素,也就是v
# 当我们reverse传入True时,降序排列,不设置或者传入False时,升序排列
res = sorted(it,key=operator.itemgetter(1),reverse=True)
print(res)

打印结果,后面的截图没取到

发布了85 篇原创文章 · 获赞 3 · 访问量 2493

猜你喜欢

转载自blog.csdn.net/Romantic_sir/article/details/104486431