python-3.x - python - 类属性的组和计数

73 2

我有一个结果类如下


class Result:


 cluster = -1;


 label = -1;



cluster和label的值都可以在09之间,我要做的是计算cluster中标签的数量,目前我正在使用下面的代码来计数,但是,它不是一个优雅的解决方案,resultList是Result对象的列表。


countZero = 0;


countOne = 0;


countTwo = 0;


countThree = 0;


countFour = 0;


countFive = 0;


countSix = 0;


countSeven = 0;


countEight = 0;


countNine = 0;



 for i in range(len(resultList)):


 if resultList[i].cluster == 0:


 if resultList[i].label == 0:


 countZero = countZero + 1


 if resultList[i].label == 1:


 countOne = countOne + 1


 if resultList[i].label == 2:


 countTwo = countTwo + 1


 if resultList[i].label == 3:


 countThree = countThree + 1


 if resultList[i].label == 4:


 countFour = countFour + 1


 if resultList[i].label == 5:


 countFive = countFive + 1


 if resultList[i].label == 6:


 countSix = countSix + 1


 if resultList[i].label == 7:


 countSeven = countSeven + 1


 if resultList[i].label == 8:


 countEight = countEight + 1


 if resultList[i].label == 9:


 countNine = countNine + 1



 print(countZero) # 


 print(countOne) # 


 print(countTwo) # 


 print(countThree) #


 print(countFour) # 


 print(countFive) # 


 print(countSix) # 


 print(countSeven) # 


 print(countEight) # 


 print(countNine) #



在寻找更好的解决方案时,任何建议或指导都将得到极大的赞赏。

时间: 原作者:

101 4

如果要获取cluster中某个标签的特定计数,可以创建cluster_id和label_id的嵌套字典:


# Create empty dictionary


cluster_dict = {}


# For 0-9 cluster_id


for cluster_id in range(10):


 # Create a dict for each cluster


 if cluster_id not in cluster_dict.keys():


 cluster_dict[cluster_id] = {}


 # For 0-9 label_id


 for label_id in range(10):


 # Set the cluster/label count to 0


 cluster_dict[cluster_id][label_id] = 0



然后,你可以使用result_list值填充它:


for res in result_list:


 cluster_dict[res.cluster][res.label] += 1



这样可以访问计数,因此对于集群0和标签2:

 
cluster_dict[0][2]



 

你还可以找到给定cluster的结果数目,而不考虑标签:


sum(cluster_dict[0].values())



你还可以找到给定标签的结果数量,而不考虑cluster:


sum([count for cluster_id, label_counter in cluster_dict.items() for label_id, count in label_counter.items() if label_id == 2])



原作者:
135 0

计数器函数返回一个字典,其中包含每个标签的计数,对cluster0使用此方法:


from collections import Counter



Counter(resultList[resultList['cluster']==0]]['label'])



原作者:
124 4

这就是数据结构的用途。在这里,你可以使用dict在几行中完成所有这些操作:


counts = {i:0 for i in range(10)} # constructs a dict {1: 0} for each number 0-9



for i in range(len(resultList)):


 if resultList[i].cluster == 0:


 counts[resultList[i].label] += 1 # find the count corresponding to the number, and increment it



for k, v in counts:


 print(f"Count {k}: {v}")



原作者:
147 2

counts = [0 for x in range(10)]



for i in range(len(resultList)):


 if resultList[i].cluster == 0:


 counts[resultList[i].label] += 1



原作者:
91 4

mach更容易


import random



class Result:


 def __init__(self ,cluster , label):


 self.label = label


 self.cluster = cluster



# global counter 


counter = {key:0 for key in range(1 , 10)}



# gen random for testing 


lists = [Result(random.randint(0 , 1) , random.randint(1 , 9)) for r in range(1000)]



for result in lists:


 counter[result.label] += 1 if result.cluster == 0 else 0



原作者:
133 3

你可以使用长度为10的列表,并使用索引来跟踪你的计数,


#List of size 10


counts = [0 for _ in range(10)]



#Iterate through result list


for result in resultList:


 # Increment the counter corresponding to each label


 counts[result.label] += 1



原作者:
90 1


from collections import defaultdict



class Result:


 def __init__(self, c, l):


 self.cluster = c


 self.label = l



counts = defaultdict(int)



resultList = [Result(1,9), Result(2,1), Result(3, 1), Result(1, 2), Result(1,9)]



for r in resultList:


 counts[(r.cluster, r.label)] += 1



print(counts)



输出: defaultdict(<class 'int'>, {(1, 9): 2, (2, 1): 1, (3, 1): 1, (1, 2): 1})

原作者:
...