对二维列表中的字符串数据中的数字排序,拿到出现数字最大的字符串,并取出出现最多的,求平均值得到结果

对于这样一个列表 取出列表中的每个列表 : 后的最大值的元素 统计 : 前的数字出现最多的 并以 : 后的数字求平均值

类似如这种的列表

 results =[['3193:99.54', '3284:0.46','3193:99.55', '3299:0.00'], ['3193:100.00', '3208:0.00', '3240:0.00'],['3193:99.54', '3284:0.46', '3299:0.00'], ['3193:100.00', '3208:0.00', '3240:0.00']]

解决办法(low)

def max_list(lt):
    """
     Take out the most elements in the list
    :param lt: list
    :return: most elements
    """
    temp = 0
    for i in lt:
        if lt.count(i) > temp:
            max_str = i
            temp = lt.count(i)
    return max_str

def test():
    new_results = []
    new_results1 = []
    for i in results:
        i.sort(reverse=True, key=lambda j: float(j.split(":")[1]))
        new_results.append(i[0].split(":")[0])
        new_results1.append(i[0])
    max_results = max_list(new_results)
    sum = 0
    num = new_results.count(max_results)
    for rid in new_results1:
        if rid.split(":")[0] == max_results:
            sum = sum + float(rid.split(":")[1])
    end_result = round(sum / num, 2)
    return end_result, max_results

有好的办法希望可以提出来 。谢谢!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值