Python Forum

Full Version: Creating new lists with names depending on a changing integer in a while loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to create a series of lists that contain paths of some distance x, until I reach a specific object which is called the target (its a graph problem where I am finding a path from the source to a target). For example, I want the list of neighbours that are distance 3 away from my source to be called new_neighbours_len3. I have tried to show this in the code below by putting the x in brackets, but I know this will not give my required result. What would I code to get the lists with the changing value of x at the end of their name?

    while pathfound == False:
        x = 2       # x denotes the 'distance' from source
        new_neighbours_len(x) = set()
        for oldneighbour in new_neighbours_len(x-1):
            neighbours_for_person(oldneighbour)     #this will give neighbours that are of length x from source
            for i in len(neighbours):
                if neighbours[i[1]] = target:
                    for j in len(potential_paths):
                        if potential_paths[j[-1][1]] == oldneighbour: #this should give me the path from source to the previous neighbour
                            path = potential_paths[j].append(neighbours[i]) #not sure if this will work because it calls 'i'
                            #from the previous for loop.
                            pathfound == True    #this means the while loop will not continue
                        else:
                            continue #maybe this should 'pass'



                else:
                    new_neighbours_len(x).add(neighbours[i[1]]) #this should add each neighbour at distance x from source to set 
                                                                #new_neighbours_len(x)
                    for j in len(potential_paths):
                        if potential_paths[j[-1][1]] == oldneighbour:
                            potential_paths.append(potential_paths[j].append(neighbours[i]))
                            #this is gonna create a larger potential paths list than is optimal, because I could try to remove the
                            #list that ended with oldneighbour (as we know this hasn't reached target if oldneighbour =! target). But
                            #I need to use this potential path when creating my new ones


        x += 1