listリストカウント方法

1462 ワード

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))