首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(python)不使用内置函数对日期进行排序

(python)不使用内置函数对日期进行排序
EN

Stack Overflow用户
提问于 2022-09-29 02:33:46
回答 2查看 50关注 0票数 2

在函数sort()中不使用build的python排序日期,但我想要的期望是

代码语言:javascript
复制
['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']      

我确实读过类似的问题,并试图解决它,但仍然是股票。

How to sort things, without using the sorted build-in function?

第一版代码:

代码语言:javascript
复制
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[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))

第二版代码:

代码语言:javascript
复制
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:
             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))

(pic)关于代码和输出第一版:

(pic)关于代码和输出第二版:

(第三版)只试着使用"input_list",删除"new_list“以节省内存--如果那样的话,我怎么做?

代码语言:javascript
复制
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))

(图)尝试使用"input_list",删除"new_list“以节省内存:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-09-29 02:45:19

尝尝这个

代码语言:javascript
复制
import datetime


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']

lst = [datetime.datetime.strptime(ts, "%Y-%m-%d") for ts in customer_date_list]

outLst = []
for _ in customer_date_list:
    m = min(lst)
    lst.remove(m)
    outLst.append(m.strftime('%Y-%m-%d'))

print(outLst)

输出

代码语言:javascript
复制
['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']

正如OP所说的,“我可以使用一个列表lst (没有outLst )保存在同一个列表上。(为了节省内存)。”

代码语言:javascript
复制
import datetime


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']

lst = [datetime.datetime.strptime(ts, "%Y-%m-%d") for ts in customer_date_list]


for i in range(len(customer_date_list)):
    m = max(lst[i:])
    lst.remove(m)
    lst.insert(0,m.strftime('%Y-%m-%d'))

print(lst)

输出

代码语言:javascript
复制
['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']
票数 2
EN

Stack Overflow用户

发布于 2022-09-29 02:39:09

实际上,您的文本日期列表几乎是ISO可排序格式,只是有时月份可能是一位数而不是两位数。如果我们给这个月留下了零,那么排序应该是有效的:

代码语言:javascript
复制
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']
output = [re.sub(r'-(\d)-', r'-0\1-', x) for x in customer_date_list]
output.sort()
print(output)

['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']
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73889704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档