栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【Python】列表合并并排序

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Python】列表合并并排序

背景:在使用echarts时因为数据比较长无法显示,我们需要相邻的两个数合并

以下是12个月每相邻两个数值合并

lists = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

n = 2  # 表示多少个一组
list = [lists[i:i + n] for i in range(0, len(lists), n)]

print(list)
line_news = []
for line in list:
    line_news.append(sum(line))

print(line_news)

结果
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]
[3, 7, 11, 15, 19, 23]

真实应用DTCloud代码

	alarm_data_list = []
        all_count = pool['fm.warning'].sudo().search_count([])
        if all_count > 0:
            warning = pool['fm.warning'].sudo().read_group(domain=domain5, fields=['id'],
                                                           groupby=['alarm_style'])
            for group in warning:
                ratio = 0
                alarm_style_count = int(group['alarm_style_count'])
                if alarm_style_count > 0 and all_count > 0:
                    ratio = alarm_style_count / all_count * 100
                data = {
                    'sequence': 0,
                    'qty': alarm_style_count,
                    'name': alarm_style_list.get(group['alarm_style']),
                    'ratio': '%s %%' % int(ratio),
                }

                alarm_data_list.append(data)

            # 从大到上排序
            if alarm_data_list:
                alarm_data_list.sort(key=lambda i: i["qty"], reverse=True)

            # 对数量进行排序
            i = 1
            alarm_data_list_news = []
            for line in alarm_data_list:
                line_news = line['sequence'] = i
                alarm_data_list_news.append(line_news)
                i += 1

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/918484.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号