Making a loop to form a list?

后端 未结 2 834
闹比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 14:54

    you return only [(tuple([service_code] + [l] + [[curr_route]]))] which is only one entry, this is why you get only:

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

    you need to store all entries in one list, say, results and append to it each entry in your loop, something like:

    results.append(tuple([service_code] + [l] + [[curr_route]]))
    

    and return:

    return results 
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题