Python Forum
For looping over a list, editing the list from inside the loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For looping over a list, editing the list from inside the loop?
#1
Hello,
I'm on a project where I want to code a random forest from scratch (to learn both how to code and the details and nuances of this type of machine learning). I know there are libraries to make stuff easily, but I want to make with my hands and learn.

WM[d] contains a list of lists and is global. d stands for the depth, r stands for the row, and each (depth, row) couple identifies a node, represented as a list containing the parameters of that node.
I am calling a node this way: WM[d][r]; and calling an element of a node via WM[d][r][elementID]

Some part of my code looks something like that:
for r in range(len(WM[d])):
    train_node(d, r)
In the first part, I have to loop over WM to create all the nodes. train_node() contains the code to create the next nodes if needs be, in the form of

def train_node(f,t,d,r):
    global WM
    if stuff:
        WM[d][r].append([]) #to create a new row element at that depth
        WM[d][r][-1] = [param1, param2...]
    return 'I love Yaks'
However it seems that my logic is flawed, since a first node is created, but the function never loops into them, meaning the nodes it created are not getting their parameters.
As I understand it, in the for loop WM is evaluated to give the range for r, and if during the loop we extend the range of r it doesn't reevaluate its range, meaning i won't train nodes that weren't there at the first call.

Am I right?
If there any way to do what I try to do cleanly? (I'm far from an experienced programmer, i'm basically selftaught, and this project has the objective of keeping selfteaching :) )
I suppose I can switch to while loops to do what I want to do here (is that even true?), but I was really interested in understanding what is going on exactly.
Reply


Messages In This Thread
For looping over a list, editing the list from inside the loop? - by Krookroo - Aug-28-2017, 12:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 442 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,726 Nov-07-2023, 09:49 AM
Last Post: buran
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,173 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  for loops break when I call the list I'm looping through Radical 4 895 Sep-18-2023, 07:52 AM
Last Post: buran
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 796 May-02-2023, 08:40 AM
Last Post: Gribouillis
  Delete strings from a list to create a new only number list Dvdscot 8 1,543 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 921 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,839 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Help adding a loop inside a loop Extra 31 4,552 Oct-23-2022, 12:16 AM
Last Post: Extra
  convert this List Comprehensions to loop jacklee26 8 1,512 Oct-21-2022, 04:25 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020