Eine Schleife bilden, um eine Liste zu bilden?

def make_services(routes_data):
    routes = []
    curr_route = []
    x = split_routes(routes_data)
    service_data1 = x[1]  # (’106’, [(’106’, ’1’, ’1’, ’43009’), ... , (’106’, ’2’, ’51’, ’43009’)])
    service_data = service_data1[1] #[(’106’, ’1’, ’1’, ’43009’), ... , (’106’, ’2’, ’51’, ’43009’)]

    first = service_data[0]  #('106', '1', '1', '43009')
    service_code = first[0]
    curr_dir = first[1]   # '1'
    l = list(curr_dir)    # ['1']


    for entry in service_data:
        direction = entry[1] #'1'
        stop = entry[3]  #'43009'

        if direction == curr_dir:   
            curr_route.append(stop) #[43009]
        else:
            routes.append(curr_route)   #[[]]
            curr_route = [stop]         #['43009']
            curr_dir = direction       #not '1'
            l.append(direction)  # ['1', __]


    routes.append(curr_route)   #[['43009']]

    #modi
    return [(tuple([service_code] + [l] + [[curr_route]]))]  

Grundsätzlich gibt mein Code mich nur zurück

[('106', ['1', '2'], [['43009', ... '43009']])]

Was ich jedoch brauche, ist

[ ('106', ['1', '2'], [['43009', ... '43009']]),
     ('171', ['1', '2'], [['59009'...'59009]]),
      ('184', ['1'], [['45009'... '45009']]) ]

x = split_routes (routes_data) gibt mich zurück

[(’171’, [(’171’, ’1’, ’1’, ’59009’), ... , (’171’, ’2’, ’73’, ’59009’)]),
(’106’, [(’106’, ’1’, ’1’, ’43009’), ... , (’106’, ’2’, ’51’, ’43009’)]),
(’184’, [(’184’, ’1’, ’1’, ’45009’), ... , (’184’, ’1’, ’52’, ’45009’)])]

Ich glaube, dass mit meiner Schleife oben etwas nicht stimmt ... Sollte es 2 Schleifen geben? Und ein Hinweis wurde gegeben, dass ich Karte verwenden könnte.

routes_data hier ist bus_stations.txt

106,1,1,43009
.
.
.
106,2,51,43009
171,1,1,59009
.
.
.
171,2,73,59009
184,1,1,45009
.
.
.
184,1,52,45009

Antworten auf die Frage(2)

Ihre Antwort auf die Frage