在Python 中, collections 模塊提供了 Counter 類,用于計(jì)算可迭代對(duì)象中元素的數(shù)量。 Counter 是一個(gè)字典的子類,它以元素作為鍵,以元素出現(xiàn)的次數(shù)作為值進(jìn)行計(jì)數(shù)。 1、創(chuàng)建計(jì)數(shù)器對(duì)象 1 2 3 4 5 from collections import Counter my_list = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] my_
www.dbjr.com.cn/python/294732b...htm 2025-6-7