Sorting 按日期对对象进行排序=T。获取日期()
遵循书中的指南: 我不明白为什么最上面的版本不按日期值对我的对象进行排序,而我让最下面的版本也这样做。顶部是对象列表,底部是字符串列表。我添加了一些print语句,这样我就可以看到什么是比较的,以确保所有内容都得到了比较,并且它们都得到了比较,但没有发生交换Sorting 按日期对对象进行排序=T。获取日期(),sorting,object,Sorting,Object,遵循书中的指南: 我不明白为什么最上面的版本不按日期值对我的对象进行排序,而我让最下面的版本也这样做。顶部是对象列表,底部是字符串列表。我添加了一些print语句,这样我就可以看到什么是比较的,以确保所有内容都得到了比较,并且它们都得到了比较,但没有发生交换 l = len(list_of_objects) for n in list_of_objects: holder = n.get_date() holder = int(holder) mp = n for i
l = len(list_of_objects)
for n in list_of_objects:
holder = n.get_date()
holder = int(holder)
mp = n
for i in list_of_objects:
top = i.get_date()
top = int(top)
print(holder, 'vs', top)
if top < holder:
mp = i
n, mp = mp, n
sort_list = []
l = len(list_of_objects)
####Sorter####
print('Sorting Data')
for T in list_of_objects:
string = str(T.get_date())+':'+str(T.get_transaction_type())+':'+str(T.get_amount())
sort_list.insert(0,string)
for bottom in range(l-1):
lower = sort_list[bottom]
holder = lower[0:8]
mp = bottom
for i in range(bottom+1,l):
top = sort_list[i]
top = top[0:8]
if top < holder:
mp = i
print(bottom, holder, top)
sort_list[bottom], sort_list[mp] = sort_list[mp], sort_list[bottom]
l=len(对象列表)
对于\u对象列表\u中的n:
持有者=n.获取日期()
holder=int(holder)
mp=n
对于\u对象列表\u中的i:
top=i.获取日期()
top=int(top)
打印(支架“vs”,顶部)
如果顶部<支架:
mp=i
n、 mp=mp,n
排序列表=[]
l=len(对象列表)
####分拣机
打印('排序数据')
对于\u对象列表\u中的T:
string=str(T.get_date())+':'+str(T.get_transaction_type())+':'+str(T.get_amount())
排序列表。插入(0,字符串)
对于范围内的底部(l-1):
下=排序列表[底部]
保持架=较低[0:8]
mp=底部
对于范围内的i(底部+1,l):
top=排序列表[i]
顶部=顶部[0:8]
如果顶部<支架:
mp=i
打印(底部、支架、顶部)
排序列表[底部],排序列表[mp]=排序列表[mp],排序列表[底部]
拧紧书本的方法。我只是简单地使用sorted()和日期的数值制作了一个序列键。然后找到了一种更新列表的方法,即在发现值匹配时将其追加到新列表中,并在使用时从旧列表中删除条目
s_列表=[]
更新的\u对象的\u列表=[]
对于\u对象列表\u中的i:
s_list.append(i.get_date())
s_列表=已排序(s_列表)
计数=0
对于s_列表中的m:
对于\u对象列表\u中的n:
日期=n.获取日期()
如果日期==m:更新了\u对象的\u列表。\u。追加(n) 列出\u对象的\u。删除(n) 计数=+1 \u对象列表=更新的\u对象列表
s_list =[]
updated_list_of_objects = []
for i in list_of_objects:
s_list.append(i.get_date())
s_list = sorted(s_list)
count = 0
for m in s_list:
for n in list_of_objects:
date = n.get_date()
if date == m:
updated_list_of_objects.append(n)
list_of_objects.remove(n)
count =+ 1
list_of_objects = updated_list_of_objects