代码之家  ›  专栏  ›  技术社区  ›  Nazim Kerimbekov Gusev Slava

从列表中的元素中获取最频繁的单词

  •  -1
  • Nazim Kerimbekov Gusev Slava  · 技术社区  · 6 年前

    我正在处理以下列表:

    ["hello, how are you", "hello", "how are you doing","are you ok"]

    我怎样才能得到每个元素中每个词的频率呢?

    列表的所有输出都应如下所示:

    you: 3
    are: 3
    hello: 2
    how: 2
    doing: 1
    ok: 1
    
    3 回复  |  直到 6 年前
        1
  •  0
  •   Sunitha    6 年前

    collections.Counter

    from intertools import Counter
    import string
    
    l=["hello, how are you", "hello", "how are you doing","are you ok"]
    
    Counter([w.strip(string.punctuation) for s in l for w in s.split() ])
    # Counter({'are': 3, 'you': 3, 'hello': 2, 'how': 2, 'doing': 1, 'ok': 1})
    
        2
  •  0
  •   Rakesh    6 年前

    collections.Counter

    from collections import Counter
    import string
    
    data = ["hello, how are you", "hello", "how are you doing","are you ok"]
    translator = str.maketrans('', '', string.punctuation)
    
    d = Counter(" ".join(data).translate(translator).split())
    #If python2
    #d = Counter(" ".join(data).translate(None, string.punctuation).split())
    
    print(d)
    

    Counter({'are': 3, 'you': 3, 'how': 2, 'hello': 2, 'doing': 1, 'ok': 1})
    
        3
  •  0
  •   Boris Reif    6 年前
    def wordListToFreqDict(wordlist):
        wordfreq = [wordlist.count(p) for p in wordlist]
        return dict(zip(wordlist,wordfreq))
    
    def sortFreqDict(freqdict):
        aux = [(freqdict[key], key) for key in freqdict]
        aux.sort()
        aux.reverse()
        return aux
    
    a = ["hello, how are you", "hello", "how are you doing","are you ok"]
    wordstring = ' '.join(a)
    wordlist = wordstring.split()
    
    wordfreq = [wordlist.count(w) for w in wordlist] # a list comprehension
    
    dictionary = wordListToFreqDict(wordlist)
    sorteddict = sortFreqDict(dictionary)
    
    for s in sorteddict: print(str(s))