list列表计数方法

def get_counts(sequence):
    counts = {}
    for x in sequence:
        if x in counts:
            counts[x] += 1
        else:
            counts[x] = 1
    return counts

def get_counts2(sequence):
    counts = defaultdict(int)# 所有值均被初始化为 0
    for x in sequence:
        counts[x] += 1
    return counts

def get_counts3(sequence):
    return dict(Counter(sequence))

你可能感兴趣的:(python)