TypeError bei Python-Programm?
Ich habe folgendes Programm: import itertools # Daten aus der Tabelle einlesen data = [] with open('tour1.txt', 'r') as file: for line in file: parts = line.strip().split(',') data.append((parts[0], int(parts[1]), parts[2] == 'X')) # Essentielle Tourpunkte auswählen essential_points = [place for place, year, is_essential in data if is_essential] # Funktion zur Berechnung der Teiltour def calculate_tour(places, essential_places): min_distance = float('inf') min_tour = None for perm in itertools.permutations(essential_places): tour = [places[0][0]] + [places[i][0] for i in perm] + [places[0][0]] total_distance = 0 for i in range(len(tour) - 1): total_distance += abs([x[1] for x in places if x[0] == tour[i]][0] - [x[1] for x in places if x[0] == tour[i + 1]][0]) if total_distance < min_distance: min_distance = total_distance min_tour = tour return min_tour # Sortieren der Orte nach ihrem Jahr sorted_data = sorted(data, key=lambda x: x[1]) # Die Teiltour berechnen shortest_tour = calculate_tour(sorted_data, essential_points) # Ergebnis ausgeben for point in shortest_tour: print(point, end=" -> ") print(shortest_tour[0]) # Zum Startpunkt zurückkehren Beim Ausführen wird mir folgender Fehler ausgegeben: TypeError: list indices must be integers or slices, not str Die Textdatei ist nach dem Format "Brauerei,1613,X,0" (als Beispiel) aufgebaut. Ich habe probiert das Problem durch ein voranstellen von Int() bei den Listen zu Lösen, was nicht funktioniert hat... Gibt es eine andere Möglichkeit zur Lösung? Vielen Dank!