What we are provided with is data in the form of a string. The data is similar to that provided below. Need the function to return a list of tuples, sorted by 'agency' (asc) and 'month' (desc)
I believe if we index this string, the first element is date, then rental agency, then unique renter id, car
2020-09-12, Budget, GD-0032-DD-XP, Ford,\n 2020-04-22, Avis, D143123, Toyota,\n 2020-04-03, Herz, 331029411, Jeep,\n 2020-10-31, HERZ, , Hyundai,\n 2020-09-10, Budget, Gd-1932-Ee-Rm, Chevrolet,\n 2020-12-01, National, 9890001, Ford,\n 2020-05-13, Alamo, W***, Hyundai,\n 2020-01-21, Enterprise, GD-8888-TL-MP, Jeep,\n 2020-01-01, Enterprise, GD-8888-TL-MP, Tesla,\n 2020-09-12, Alamo, GD-8888-TO-MP,....
CodePudding user response:
String Splitting
String splitting can in Python can be done via the .split(delimiter) function of any String. the split()-function returns a list of String excluding the string at which the original String was split. Examples:
> "Hello World!".split("o")
["Hell", " W", "rld!"]
As one can perceive, the String "o", at which the string was split, is not contained in the splits.
First split in single lines, then split the line at the commas and then split the date.
filtered_list: List[Tuple[int, str]] = []
data: str # data is the string containing all you
lines: List[str] = data.split("\n")
# This splits your data-string at each newline-character
for line in lines: # Iterate over each line
date, agency, id, car, _ = line.split(",")
# Split each line at the ','.
# Works only if the data is formatted they it is in your post
# The underscore is to discard the last element of the string, because there is an additional comma.
month = date.split("-")[1]
# Splitting a date like "2005-12-24" at the "-" leads to the month being in the second part of the splits
filtered_list.append((int(month), agency))
# simply append the tuple to the list
pass # Iteration END
# Here you can do whatever you want with your list, return it, for example
CodePudding user response:
I think you need splitlines
and sorted
like this
a = """2020-09-12, Budget, GD-0032-DD-XP, Ford,\n 2020-04-22, Avis, D143123, Toyota,\n 2020-04-03, Herz, 331029411, Jeep,\n 2020-10-31, HERZ, , Hyundai,\n 2020-09-10, Budget, Gd-1932-Ee-Rm, Chevrolet,\n 2020-12-01, National, 9890001, Ford,\n 2020-05-13, Alamo, W***, Hyundai,\n 2020-01-21, Enterprise, GD-8888-TL-MP, Jeep"""
def list_of_tuple(text):
return sorted((tuple(data.split(",")[:2])for data in text.splitlines()), key=lambda x: (x[1], -int(x[0].split("-")[1].strip())))
print(list(list_of_tuple(a))) # this is sorted by agency first and month descending as you can check in output
# [(' 2020-05-13', ' Alamo'), (' 2020-04-22', ' Avis'), ('2020-09-12', ' Budget'), (' 2020-09-10', ' Budget'), (' 2020-01-21', ' Enterprise'), (' 2020-10-31', ' HERZ'), (' 2020-04-03', ' Herz'), (' 2020-12-01', ' National')]