美文网首页
Python-其他库/模块

Python-其他库/模块

作者: JUNjianshuZHU | 来源:发表于2018-05-25 16:35 被阅读0次

    1.collections库-累加器

    from collections import Counter
    
    doc = 'Python is an interpreted high-level programming language for general-purpose programming. '
    
    #把doc按空格切割
    word_list = doc.split()
    
    #调用Counter()函数对单词进行累加
    cc = Counter(word_list)
    cc
    --->
    Counter({'Python': 1,
             'an': 1,
             'for': 1,
             'general-purpose': 1,
             'high-level': 1,
             'interpreted': 1,
             'is': 1,
             'language': 1,
             'programming': 1,
             'programming.': 1})
    
    #迭代输出结果
    for k,v in cc.most_common():
        print(k,v)
    --->
    Python 1
    is 1
    an 1
    interpreted 1
    high-level 1
    programming 1
    language 1
    for 1
    general-purpose 1
    programming. 1
    

    相关文章

      网友评论

          本文标题:Python-其他库/模块

          本文链接:https://www.haomeiwen.com/subject/rddujftx.html