Edgar Leong
Edgar Leong

Reputation: 23

How to loop through a constantly updating list?

for i in range(0, len(list_stops)):
    for j in range(1, len(list_stops[i])):
        current = stops_from_stop(list_stops[i][j])
        list_stops.extend(current)
        for k in range(0, len(current)):
            for m in range(0, len(current[k])):
                list_stops_x.extend([current[k][m]])
            if id_b in list_stops_x:
                #rest of code

list_stops is a list of lists. Eg [[1,2,3], [4,5,6]]

list_stops_x is a single list of all the numbers in list_stops. Eg [1,2,3,4,5,6]. Basically used as a condition to enter the rest of the code with a return statement at the end, so the loop does not repeat.

I find that the loop ends after reaching the final index of the first version of list_stops, but I am constantly extending list_stops and want the loop to carry on into the extensions. So for example if I extend [7,8,9] to list_stops in the loop, if id_b is not found I still want it to loop through [7,8,9], but it stops at 6.

Note: This is only a section of the entire function, but I am quite sure the problem lies here. This is also for an introductory programming course, so a simple solution will do :) The full code is below

def find_path(stops, routes, stop_a, stop_b):
    id_a = stop_a[0]
    id_b = stop_b[0]
    unused = unused_stops()
    if id_b in list(unused):
        return []
    total_route = list()
    all_stops = stops_from_stop(id_a)
    list_stops_x = stops_from_stop_x(id_a)
    list_stops = stops_from_stop(id_a)
    for index in range(0, len(all_stops)):
        if id_b in all_stops[index]:
            return find_route(stops, routes, stop_a, stop_b)
    for i in range(0, len(list_stops)):
        for j in range(1, len(list_stops[i])):
            current = stops_from_stop(list_stops[i][j])
            list_stops.extend(current)
            for k in range(0, len(current)):
                for m in range(0, len(current[k])):
                    list_stops_x.extend([current[k][m]])
                    if id_b in list_stops_x:                 
                        stops_used_rev = [id_b]
                        last_route = list_stops[len(list_stops) - 1]
                        current_stop = last_route[0]
                        stops_used_rev += [current_stop]
                        for i in range(0, len(list_stops)):
                            if (current_stop in list_stops[i]) and (list_stops[i][0] == id_a):
                                stops_used_rev += [id_a]
                                break
                            elif current_stop in list_stops[i]:
                                current_stop = last_route[0]
                                stops_used_rev += [current_stop]
                        stops_used = stops_used_rev[::-1]               
                        for index in range(0, len(stops_used) - 1):                     
                            total_route.extend(find_route(stops, routes, stops[stops_used[index]], stops[stops_used[index + 1]]))
                        return total_route

stops_from_stop finds the list stops accessible from the current stop and appends to another list. stops_from_stops_x does the same but extends instead

Upvotes: 1

Views: 628

Answers (1)

Shadesfear
Shadesfear

Reputation: 749

So the issue is that we use the range(0, len(list_stops)) if we instead use enumerate in the following way, BEWARE INFINITE LOOP, due too we keep adding elements to the list. So be careful, but this should give the desired result. I have changed some part of the code so I could run it.

Enumerate makes it possible to both get the item of the list (val) and the index that we are currently at.

list_stops = [[1,2,3], [4,5,6]]
list_stops_x = [1,2,3,4,5,6]
newer = [[7, 8, 9], [10, 11, 12]]
id_b = 9

for i, val in enumerate(list_stops):
    print(val)
    for j in range(1, len(list_stops[i])):

        current = newer
        list_stops.extend(current)

        for k in range(0, len(current)):
            for m in range(0, len(current[k])):
                list_stops_x.extend([current[k][m]])

            # if id_b in list_stops_x:
            #     print("id_b in")
            #     break

EDIT 1

in your code that was in the edit suggestion you have loops that look like the following:

for i in range(0, len(list_stops)):
   ...

These should be replace with the following to fix the issue

for i, val in enumerate(list_stops):
   ...

I have a difficult time of testing the code, but try replace the for loops with the type that i provided and let me know

Edit 2

If we keep adding to the list ofcourse it will loop infinitely unless we exit somewhere. You can see this example that I add elements to list a from list b and then remove the item. As you see from the output we loop eight times so we know it works, you should reflect such changes in your code

a = [1, 2, 3, 4]
b = [5, 6, 7, 8]


for idx, val in enumerate(a):
    print("Loop Count: " + str(idx))
    if b:
        a.append(b[0])
        b.remove(b[0])
Loop Count: 0
Loop Count: 1
Loop Count: 2
Loop Count: 3
Loop Count: 4
Loop Count: 5
Loop Count: 6
Loop Count: 7

Upvotes: 1

Related Questions