I have a csv file where I group rows together if they share the same address. These groups contain around 1 - 10 rows. I need to sort these groups based on a date in the fourth column in the first row of each group.
Below is a pseudo illustration of my data and below that is my code.
list_of_parcel_groups = [ [ [ [0,1,2,11/22/2022],[0,1,2,01/01/2001] ] , [ [0,1,2,3/11/2022],[0,1,2,3/4/2016],[0,1,2,5/18/2011],[0,1,2,03/13/2009] ] , [ [0,1,2,5/13/2019],[0,1,2,4/20/2018],[0,1,2,7/13/1999] ] ]
I want to sort list_of_parcel_groups and append it to qualified so
that it looks like this...
qualified = [ [0,1,2,5/13/2019],[0,1,2,4/20/2018],[0,1,2,7/13/1999],[0,1,2,3/11/2022],[0,1,2,3/4/2016],[0,1,2,5/18/2011],[0,1,2,03/13/2009],[0,1,2,11/22/2022],[0,1,2,01/01/2001] ]
I am sorting based on the dates in list_of_parcel_groups[0][0][3], list_of_parcel_groups[1][0][3], and list_of_parcel_groups[2][0][3].
for i in range(len(list_of_parcel_groups)):
if i == 0:
string_input_with_date = list_of_parcel_groups[i][0][3]
date = datetime.strptime(string_input_with_date, "%m/%d/%Y")
for item in list_of_parcel_groups[i]:
qualified.append(item)
continue
string_input_with_date1 = list_of_parcel_groups[i][0][3]
date1 = datetime.strptime(string_input_with_date1, "%m/%d/%Y")
if (date1.date() <= date.date()):
list_of_parcel_groups[i].reverse()
for item in list_of_parcel_groups[i]:
qualified.insert(0,item)
list_of_parcel_groups[i].reverse()
date = string_input_with_date = list_of_parcel_groups[i][0][3]
date = datetime.strptime(string_input_with_date, "%m/%d/%Y")
continue
if (date1.date() > date.date()):
for item in list_of_parcel_groups[i]:
qualified.append(item)
date = string_input_with_date = list_of_parcel_groups[i][0][3]
date = datetime.strptime(string_input_with_date, "%m/%d/%Y")
I tried inserting each element of a group into index 0 of the array whenever it was earlier date than the previous date, and appending each row the group to the end of the array whenever the date was later than the previous date.
This did not give me the sorted result I was trying to do.
Also note each group is already sorted by date in the correct manner. This is why when I use the insert() function I reverse() the group and I reverse() it again after the insert() so that I can still use list_of_parcel_groups[i][0][3] to assign to the variable 'date' so that I can compare it to the date in the next loop.
CodePudding user response:
If I've correctly understood the construction of your raw data, then this should give you what you're looking for - no?
from datetime import datetime
list_of_parcel_groups = [[[0,1,2,'11/22/2022'], [0,1,2,'1/01/2001']] ,
[[0,1,2,'3/11/2022'], [0,1,2,'3/04/2016'], [0,1,2,'5/18/2011'], [0,1,2,'3/13/2009']],
[[0,1,2,'5/13/2019'], [0,1,2,'4/20/2018'], [0,1,2,'7/13/1999']]]
qualified = sorted(list_of_parcel_groups, key = lambda x: datetime.strptime(x[0][3], "%m/%d/%Y"))
for sublist in qualified:
print (sublist)
# Result:
# [[0, 1, 2, '5/13/2019'], [0, 1, 2, '4/20/2018'], [0, 1, 2, '7/13/1999']]
# [[0, 1, 2, '3/11/2022'], [0, 1, 2, '3/04/2016'], [0, 1, 2, '5/18/2011'], [0, 1, 2, '3/13/2009']]
# [[0, 1, 2, '11/22/2022'], [0, 1, 2, '1/01/2001']]