7 8 9 10 fromcollections import Counter n = ['a','b','a','c','d','a'] # 统计list中每个元素出现的个数 eleCounts = Counter(n) # most_common()返回出现次数排名前n个的元素,不输入时默认按照出现次数对所有数据排序 top_one = eleCounts.most_common(2) print(top_one) [('a', 3),...
使用collections模块中的Counter类来统计列表中每个元素的数量。 下面是一个示例: fromcollectionsimportCounter# 初始化列表my_list=[1,2,3,1,2,3,1,2,1,2,3,3]# 使用 Counter 统计每个元素的数量counts=Counter(my_list)# 输出结果print(counts)# 输出:Counter({1: 4, 2: 4, 3: 3}) 在上面的代码...
if k == -1: # Black used for noise. col = [0, 0, 0, 1] # 统计各类点的数量 count=list(my_labels).count(k) print("label:{},count:{}".format(k,count)) 1. 2. 3. 4. 5. 6. 7. 8. 9.
if list1[i] != list1[i-1]: result[i] = 0 else: result[i] = result[i-1] + 1 print(result) 1. 2. 3. 4. 5. 6. 7. 8. 9. 后来月神还给了一个逻辑性比较强的解法,代码如下所示: list1 = [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1] result = [0] *...