Thursday, September 17, 2015

Python: math(11)

Abstract: Summarize data from a list

The result:
Input of groupby(): ['aa', 'bv', 'ba', 'abc', '723', 'bcd', 'abcde', 'zc', 'n', 'v']
2 ['aa', 'bv', 'ba']
3 ['abc', '723', 'bcd']
5 ['abcde']
2 ['zc']
1 ['n', 'v']
Count frequency of elements in a set
Counter({4: 3, 5: 3, 3: 2, 76: 2, 32: 1, 1: 1, 2: 1, 6: 1, 67: 1})
ok

The script:
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 14 11:27:19 2015

@author: yuan
"""




import itertools
import collections


if __name__=="__main__":
    ####itertools.groupby()
    a = ['aa', 'bv','ba', 'abc', '723', 'bcd', 'abcde','zc','n','v']
    a_group={}
    print 'Input of groupby():', a
    for x,y in itertools.groupby(a,len):
        a_group[x]=list(y)
        print x, a_group[x]
  
    ####collections.Counter()
    print 'Count frequency of elements in a set'
    a=[1,3,4,5,76,32,67,2,4,5,4,3,5,6,76]
    a_dict=collections.Counter(a)
    print a_dict
   
    #
    print 'ok'

No comments:

Post a Comment