在函数sort()中不使用build的python排序日期,但我想要的期望是
['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?
第一版代码:
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))
第二版代码:
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“以节省内存--如果那样的话,我怎么做?
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“以节省内存:

发布于 2022-09-29 02:45:19
尝尝这个
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)输出
['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 )保存在同一个列表上。(为了节省内存)。”
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)输出
['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']发布于 2022-09-29 02:39:09
实际上,您的文本日期列表几乎是ISO可排序格式,只是有时月份可能是一位数而不是两位数。如果我们给这个月留下了零,那么排序应该是有效的:
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']https://stackoverflow.com/questions/73889704
复制相似问题