You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

不使用内置sort()函数实现Python日期排序及代码问题求助

不使用内置sort()实现日期原地排序问题解决

需求说明

要实现不使用Python内置sort()函数对日期字符串列表进行原地排序,预期输出为:

['2010-01-12', '2010-01-14', '2010-02-07', '2010-02-11', '2010-11-16',
'2010-11-22', '2010-11-23', '2010-11-26', '2010-12-02', '2010-12-13',
'2011-02-04', '2011-06-02', '2011-08-05', '2011-11-30']

现有代码错误分析

第一版代码错误

import datetime

def date_sorting_operation(input_list):
  
  input_list = [datetime.datetime.strptime(ts, "%Y-%m-%d") for ts in input_list] 

  for i in range(len(input_list)):
    for j in range(i + 1, len(input_list)):

        if l[i] > l[j]:  # 错误:使用未定义变量l,应为input_list
           l[i], l[j] = l[j], l[i]  
  return input_list

customer_date_list = ['2011-06-02', '2011-08-05', '2011-02-04', '2010-1-14', '2010-12-13', '2010-1-12', '2010-2-11', '2010-2-07', '2010-12-02', '2011-11-30']

print (date_sorting_operation(customer_date_list))
  • 变量名错误:用了未定义的l,需替换为input_list
  • 未实现原地排序:重新赋值input_list会创建新列表,原列表不会被修改
  • 返回格式错误:返回的是datetime对象列表,不是原字符串格式

第二版代码错误

import datetime

def date_sorting_operation(input_list):
  
  input_list = [datetime.datetime.strptime(ts, "%Y-%m-%d") for ts in input_list] 

  for i in range(len(input_list) - 1):
     for j in range(0, len(input_list) - i - 1):
         if input_list[j].repeats > input_list[j + 1].repeats:  # 错误:datetime对象无repeats属性
             input_list[j], input_list[j + 1] = input_list[j + 1], input_list[j] 
  return input_list

customer_date_list = ['2011-06-02', '2011-08-05', '2011-02-04', '2010-1-14', '2010-12-13', '2010-1-12', '2010-2-11', '2010-2-07', '2010-12-02', '2011-11-30']

print (date_sorting_operation(customer_date_list))
  • 属性错误:datetime.datetime对象没有repeats属性,直接比较input_list[j] > input_list[j+1]即可
  • 同样存在创建新列表、返回datetime对象而非字符串的问题,未实现原地排序

第三版代码错误

import datetime
 
def date_sorting_operation(input_list):
  
  new_list = []
  dates = [datetime.datetime.strptime(ts, "%Y-%m-%d") for ts in input_list] 
 
  while input_list:
      min = input_list[0]  
      for x in input_list: 
          if x < min:  # 错误:直接比较字符串日期,格式不统一会导致排序错误
              min = x
      new_list.append(min)
      input_list.remove(min)    
  return new_list
 
customer_date_list = ['2011-06-2', '2011-08-05', '2011-02-04', '2010-1-14', '2010-12-13', '2010-1-12', '2010-2-11', '2010-2-07', '2010-12-02', '2011-11-30']
 
print (date_sorting_operation(customer_date_list))
  • 排序逻辑错误:直接比较日期字符串,单/双数字格式会导致比较结果不符合实际日期顺序
  • 未使用转换后的dates列表,而是直接操作原字符串列表
  • 未实现原地排序:创建了new_list并修改原输入列表,逻辑混乱

正确的原地日期排序实现

采用冒泡排序实现原地排序,比较时将字符串转为datetime对象判断大小,排序后可统一日期格式:

import datetime

def date_sorting_operation(input_list):
    n = len(input_list)
    # 原地冒泡排序
    for i in range(n - 1):
        swapped = False
        for j in range(0, n - i - 1):
            # 转换为datetime对象进行日期比较
            date_j = datetime.datetime.strptime(input_list[j], "%Y-%m-%d")
            date_j1 = datetime.datetime.strptime(input_list[j+1], "%Y-%m-%d")
            if date_j > date_j1:
                # 原地交换元素
                input_list[j], input_list[j+1] = input_list[j+1], input_list[j]
                swapped = True
        # 无交换说明已排序完成,提前退出
        if not swapped:
            break
    # 统一日期格式为YYYY-MM-DD(可选)
    for idx in range(len(input_list)):
        dt = datetime.datetime.strptime(input_list[idx], "%Y-%m-%d")
        input_list[idx] = dt.strftime("%Y-%m-%d")

customer_date_list = ['2011-06-02', '2011-08-05', '2011-02-04', '2010-1-14', '2010-12-13', '2010-1-12', '2010-2-11', '2010-2-07', '2010-12-02', '2011-11-30']
date_sorting_operation(customer_date_list)
print(customer_date_list)

代码说明

  • 原地排序:直接在传入的列表上交换元素,不创建新列表
  • 准确比较:通过datetime对象判断日期大小,不受输入格式(单/双数字)影响
  • 效率优化:加入swapped标志,排序完成后提前终止循环
  • 格式统一:可选步骤将日期格式化为补零的YYYY-MM-DD,与预期输出完全一致

执行后输出:

['2010-01-12', '2010-01-14', '2010-02-07', '2010-02-11', '2010-12-02', '2010-12-13', '2011-02-04', '2011-06-02', '2011-08-05', '2011-11-30']

内容的提问来源于stack exchange,提问作者j ton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 09:10:22