按月或指定日期范围分组
创始人
2024-08-23 06:30:18
0

以下是一个示例代码,用于按月或指定日期范围分组数据:

import datetime

# 假设数据以日期为键,值为某个指标的列表
data = {
    datetime.date(2022, 1, 1): [1, 2, 3],
    datetime.date(2022, 1, 5): [4, 5, 6],
    datetime.date(2022, 2, 10): [7, 8, 9],
    datetime.date(2022, 3, 15): [10, 11, 12],
    datetime.date(2022, 4, 20): [13, 14, 15],
    datetime.date(2022, 4, 25): [16, 17, 18]
}

# 按月分组
monthly_groups = {}
for date, values in data.items():
    month = date.strftime("%Y-%m")
    if month not in monthly_groups:
        monthly_groups[month] = []
    monthly_groups[month].extend(values)

print("按月分组结果:")
for month, values in monthly_groups.items():
    print(f"{month}: {values}")

# 指定日期范围分组
start_date = datetime.date(2022, 2, 1)
end_date = datetime.date(2022, 4, 30)
range_groups = {}
for date, values in data.items():
    if start_date <= date <= end_date:
        if date not in range_groups:
            range_groups[date] = []
        range_groups[date].extend(values)

print("\n指定日期范围分组结果:")
for date, values in range_groups.items():
    print(f"{date}: {values}")

以上代码首先定义了一个示例数据字典,其中键是日期,值是某个指标的列表。然后使用两个循环分别按月和指定日期范围进行分组。

按月分组的逻辑是先将日期转换成字符串形式的年份和月份,然后以年-月作为键,将对应的值列表添加到相应的分组中。

指定日期范围分组的逻辑是使用一个起始日期和结束日期,遍历数据字典中的每个日期,如果日期在指定范围内,则将对应的值列表添加到分组中。

最后,分别输出按月分组和指定日期范围分组的结果。

相关内容

热门资讯

透视模拟器!aapoker如何... 透视模拟器!aapoker如何设置胜率(透视)aapoker发牌逻辑,专业教程(有挂插件);透视模拟...
透视脚本!aapoker公共底... 透视脚本!aapoker公共底牌(透视)aapoker怎么控制牌,可靠教程(有挂详情);1、上手简单...
透视计算!aapoker公共底... 透视计算!aapoker公共底牌(透视)aapoker透视脚本下载,切实教程(有挂脚本)1、下载好a...
透视透视!aapoker真的假... 透视透视!aapoker真的假的(透视)aapoker插件下载,2025新版(有挂教程)1、aapo...
透视能赢!aa poker透视... 透视能赢!aa poker透视软件(透视)aapoker真的假的,德州教程(有挂规律)1、每一步都需...
透视软件!aapoker脚本怎... 透视软件!aapoker脚本怎么用(透视)aapoker俱乐部靠谱吗,线上教程(有挂方法)1、进入到...
透视实锤!aapoker发牌逻... 透视实锤!aapoker发牌逻辑(透视)aapoker怎么拿好牌,2025新版教程(有挂教程)1、每...
透视线上!aapoker辅助软... 透视线上!aapoker辅助软件合法吗(透视)aapoker辅助器是真的吗,规律教程(有挂辅助);进...
透视插件!aapoker透视脚... 透视插件!aapoker透视脚本(透视)aapoker辅助工具安全吗,实用技巧(有挂规律);1、玩家...
透视数据!wepoker免费脚... 透视数据!wepoker免费脚本咨询,wepoker可以透视码(详细开辅助能查到)1、进入游戏-大厅...