python 统计数组内元素出现次数

def get_counts(sequence):
    counts={}
    for x in sequence:
        if x in counts:
            counts[x] += 1
        else:
            counts[x] = 1
    return counts
aa=['a','b','c','d','a','g']
print get_counts(aa)

C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/analyze/a6.py
{'a': 2, 'c': 1, 'b': 1, 'd': 1, 'g': 1}

 

你可能感兴趣的:(python数据分析)