python篇---删除列表指定索引,并统计标签的数量

方法一:复杂

result_dict = {                                                  
    "cls": [0, 1, 0, 3, 3],                                      
    "pro": [0.826, 0.501, 0.876, 0.345, 0.862],                  
    "x":   [1102, 1123, 345, 567, 345],                          
    "y":   [563, 123, 345, 621, 321],                            
    "w":   [81, 40, 60, 42, 56],                                 
    "h":   [93, 54, 67, 84, 45]                                  
}                                                                
                                                                 
                                                                 
cls, pro, x, y, w, h = result_dict["cls"], result_dict["pro"], result_dict["x"], result_dict["y"], result_dict["w"], result_dict["h"]


# # 方法一:精简
# for i in range(len(cls) -1, -1, -1):
#     if pro[i] < 0.6 or cls[i] == "3":
#         del cls[i], pro[i], x[i], y[i], w[i], h[i]

# 方法二:复杂, 有冗余代码
delete_index = []
for i, pro_ in enumerate(pro):
    if pro_ < 0.6 :
        delete_index.append(i)

if delete_index:
    for j in delete_index[::-1]:
        del pro[j], cls[j], x[j], y[j], w[j], h[j]

delete_index1 = []
for i, cls_ in enumerate(cls):
    if cls_ == "3":
        delete_index1.append(i)

if delete_index1:
    for j in delete_index1[::-1]:
        del pro[j], cls[j], x[j], y[j], w[j], h[j]

print(result_dict)
new_cls = result_dict["cls"]
set_cls = set(new_cls)
result_cls = {}
for i in set_cls:
    result_cls[i] = new_cls.count(i)
print(result_cls)

for a in set_cls:
    if a == "0":
        result_cls[a] = [result_cls[a], "塑料袋"]
    elif a == "3":
        result_cls[a] = [result_cls[a], "木棍"]

for i in set_cls:
    print("标签为%s的数量为%d" % (result_cls[i][1], result_cls[i][0]))

在这里插入图片描述

方法二:精简

import pandas as pd

result_dict = {
    "cls": [0, 1, 0, 3, 3],
    "pro": [0.826, 0.501, 0.876, 0.345, 0.862],
    "x":   [1102, 1123, 345, 567, 345],
    "y":   [563, 123, 345, 621, 321],
    "w":   [81, 40, 60, 42, 56],
    "h":   [93, 54, 67, 84, 45]
}


cls, pro, x, y, w, h = result_dict["cls"], result_dict["pro"], result_dict["x"], result_dict["y"], result_dict["w"], result_dict["h"]

for j in range(len(cls) - 1, -1, -1):
    if pro[j] < 0.6:
        del pro[j], cls[j], x[j], y[j], w[j], h[j]

print(result_dict)

new_result_dict = {a: result_dict["cls"].count(a) for a in set(result_dict["cls"])}
print(new_result_dict)

for a in set(result_dict["cls"]):                                              
    if a == 0:
        new_result_dict[a] = [new_result_dict[a], "塑料袋"]
    elif a == 3:
        new_result_dict[a] = [new_result_dict[a], "木棍"]

for i in set(new_result_dict):
    print("标签为%s的数量为%d" %(new_result_dict[i][1], new_result_dict[i][0]))

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心惠天意

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值