How to count the frequency of the elements in an unordered list?

后端 未结 30 2878
时光说笑
时光说笑 2020-11-22 02:37

I need to find the frequency of elements in an unordered list

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]

output->

b =         


        
30条回答
  •  一生所求
    2020-11-22 03:06

    i'm using Counter to generate a freq. dict from text file words in 1 line of code

    def _fileIndex(fh):
    ''' create a dict using Counter of a
    flat list of words (re.findall(re.compile(r"[a-zA-Z]+"), lines)) in (lines in file->for lines in fh)
    '''
    return Counter(
        [wrd.lower() for wrdList in
         [words for words in
          [re.findall(re.compile(r'[a-zA-Z]+'), lines) for lines in fh]]
         for wrd in wrdList])
    

提交回复
热议问题