简体   繁体   中英

return statement is returning nothing but print does

I have a code where the return statement seems to return nothing, but if I add a print statement directly above the return statement, the print statement works. Ex:

return(test_list) #returns None

but if I add the print statement

print(test_list)

return(test_list) #print, prints the desired result

How is this possible?

The function:

def reachable_destinations(iata_src: str, allowed: int, routes)\
    -> List[Set[str]]:

    """The first parameters represents an IATA code. The second parameter is 
    the maximum number of direct flights allowed. The last parameter represents
    route information. Return a list of the sets of IATA codes reachable from
    the first parameter in steps from 0 up to (and including) the maximum 
    number of hops."""    

    #For example, with IATA code 'AA1', maximum number of flights as 2, and the following route information
    #{'AA1': {'AA2', 'AA4'}, 'AA2': {'AA3'}, 'AA3': {'AA4', 'AA1'}, 'AA4': {'AA1'}}
    #the result should be [{'AA1'}, {'AA2', 'AA4'}, {'AA3'}].
    i = 0
    reachable_list = [{iata_src}]
    #print(reachable_list)
    for i in range(allowed):
        #print(i)
        total_set = set()
        #print(total_set)
        for j in range(len(reachable_list[i])):
            #print(j)
            #print(routes[list(reachable_list[i])[j]])
            total_set = total_set | routes[list(reachable_list[i])[j]]
            #print(total_set)
        dif = total_set - arbitrary_union(reachable_list)
        #print(dif)
        if dif == set():
            print('dif is empty')
            return reachable_list
        else:
            reachable_list.append(dif)   
            #print(reachable_list)
    print(reachable_list)        
    return reachable_list   

You could use a recursive function to solve this kind of problem:

def destinations_reachable(departure, routes, result_accumulated, depth_allowed, depth_reached=0):
    if depth_reached + 1 > len(result_accumulated):
        result_accumulated.extend(set() for x in range(depth_reached + 1 - len(result_accumulated)))
    result_accumulated[depth_reached].add(departure)
    local_destinations_reachable = routes.get(departure, set())

    if depth_reached < depth_allowed :
        for next_destination in local_destinations_reachable:
            destinations_reachable(next_destination, routes, result_accumulated, depth_allowed, depth_reached + 1)


routes_references = {'AA1': {'AA2', 'AA4'}, 'AA2': {'AA3'}, 'AA3': {'AA4', 'AA1'}, 'AA4': {'AA1'}}
result = []
depth_maximum = 2

destinations_reachable('AA1', routes_references, result, depth_maximum)

for hops in enumerate(result):
    destinations = ", ".join(hops[1])
    print(f'Destinations reachable after {hops[0]} hops: {destinations}')

print('Reachable destinations:', result)

It gives the output:

Destinations reachable after 0 hops: AA1
Destinations reachable after 1 hops: AA4, AA2
Destinations reachable after 2 hops: AA3, AA1
Reachable destinations: [{'AA1'}, {'AA4', 'AA2'}, {'AA3', 'AA1'}]

The only problem with this solution is if you want all the routes for a big number of hops we could reach the limits of the number of time a function can call itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM