The first part of my homework was to write code that does the following:
def car_make_and_models(all_cars: str) -> list:
"""
Create a list of structured information about makes and models.
For each different car make in the input string an element is created in the output list.
The element itself is a list, where the first position is the name of the make (string),
the second element is a list of models for the given make (list of strings).
No duplicate makes or models should be in the output.
The order of the makes and models should be the same os in the input list (first appearance).
"""
if not all_cars:
return []
model_list = []
cars = all_cars.split(",")
for car in cars:
car_make = car.split(" ")[0]
car_model = " ".join(car.split(" ")[1:])
car_makes = [item[0] for item in model_list]
if car_make not in car_makes:
model_list.append([car_make, [car_model]])
elif car_model not in model_list[car_makes.index(car_make)][1]:
model_list[car_makes.index(car_make)][1].append(car_model)
return model_list
"Audi A4,Skoda Super,Skoda Octavia,BMW 530,Seat Leon Lux,Skoda Superb,Skoda Superb,BMW x5"
=>
[['Audi', ['A4']], ['Skoda', ['Super', 'Octavia', 'Superb']], ['BMW', ['530', 'x5']], ['Seat', ['Leon Lux']]]
In part 2, it is required to write a function add_cars with two parameters, where the 1st car_list is the output of the function above, and the 2nd all_cars is a string, for example "Audi A5,Audi A6". So, print(add_cars([['Audi', ['A4']], ['Skoda', ['Superb']]], "Audi A6,BMW A B C,Audi A4")) should give me [['Audi', ['A4', 'A6']], ['Skoda', ['Superb']], ['BMW', ['A B C']]]. However, I don't understand how to use the function above to write the add_cars function. Can someone help or guide?