连续最长日期的记录是多少天?

2026-04-30 21:031阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计204个文字,预计阅读时间需要1分钟。

连续最长日期的记录是多少天?

pythonfrom datetime import datetime

def calculate_consecutive_dates(dates_str: str) -> int: max_consecutive_days=0 temp_flag=0 dates_list=dates_str.split(',') dates=[datetime.strptime(d, '%Y-%m-%d') for d in dates_list] print(dates) date_ints= return max_consecutive_days

from datetime import datetime
def calculate_consecutive_dates(dates_str: str):
max_consecutive_days = 0
temp_flag = 0
dates_list = dates_str.split(",")
dates = [datetime.strptime(d, "%Y-%m-%d") for d in dates_list]
print(dates)
date_ints = [d.toordinal() for d in dates]
print(date_ints)

retlist = list()
count = 1
# Avoid Index Error
for i in range(len(date_ints) - 1):
# Check if the next number is consecutive
if date_ints[i] + 1 == date_ints[i + 1]:
count += 1
else:
print(count)
# If it is not append the count and restart counting
retlist.append(count)
count = 1
# In case we stop the loop earlier then we should append the last count
retlist.append(count)
retlist.sort(reverse=True)
print(retlist)
# return retlist[0]
days_str = '''2021-11-17,2021-11-19,2021-11-20,2021-11-21,2021-11-23,2021-11-24'''
#连续最长日期
calculate_consecutive_dates(days_str)

连续最长日期的记录是多少天?

本文共计204个文字,预计阅读时间需要1分钟。

连续最长日期的记录是多少天?

pythonfrom datetime import datetime

def calculate_consecutive_dates(dates_str: str) -> int: max_consecutive_days=0 temp_flag=0 dates_list=dates_str.split(',') dates=[datetime.strptime(d, '%Y-%m-%d') for d in dates_list] print(dates) date_ints= return max_consecutive_days

from datetime import datetime
def calculate_consecutive_dates(dates_str: str):
max_consecutive_days = 0
temp_flag = 0
dates_list = dates_str.split(",")
dates = [datetime.strptime(d, "%Y-%m-%d") for d in dates_list]
print(dates)
date_ints = [d.toordinal() for d in dates]
print(date_ints)

retlist = list()
count = 1
# Avoid Index Error
for i in range(len(date_ints) - 1):
# Check if the next number is consecutive
if date_ints[i] + 1 == date_ints[i + 1]:
count += 1
else:
print(count)
# If it is not append the count and restart counting
retlist.append(count)
count = 1
# In case we stop the loop earlier then we should append the last count
retlist.append(count)
retlist.sort(reverse=True)
print(retlist)
# return retlist[0]
days_str = '''2021-11-17,2021-11-19,2021-11-20,2021-11-21,2021-11-23,2021-11-24'''
#连续最长日期
calculate_consecutive_dates(days_str)

连续最长日期的记录是多少天?