Making a loop to form a list?

后端 未结 2 870
闹比i
闹比i 2021-01-25 14:27
def make_services(routes_data):
    routes = []
    curr_route = []
    x = split_routes(routes_data)
    service_data1 = x[1]  # (’106’, [(’106’, ’1’, ’1’, ’43009’), ..         


        
2条回答
  •  心在旅途
    2021-01-25 15:02

    I think you need something more like:

    def make_services(routes_data):
        output = []
        for service in split_routes(route_data):
            # service == (’171’, [(’171’, ’1’, ’1’, ’59009’), ... , (’171’, ’2’, ’73’, ’59009’)])
            code = service[0] # 171
            directions = []
            route = []
            for stop in service[1]:
                # stop == (’171’, ’1’, ’1’, ’59009’)
               if stop[1] not in directions:
                   directions.append(stop[1])
               route.append(stop[3])
            output.append((code, directions, route))
        return output
    

    Each item in output will be a three-tuple of the code, the directions list and the route list, e.g.

    output == [('171', ['1', '2'], ['59009', ..., '59009']), ... ]
    

    You could consider making (some of) these values int() for further processing, or you can leave them as strings.

提交回复
热议问题