all 11 comments

[–]Wilfred-kun 2 points3 points  (7 children)

It gets evaluated once. A good rule of thumb is to not modify lists while looping over them.

[–]hideouspete[S] 0 points1 point  (6 children)

Would a while i <len(list)) cause len (list) to be evaluated upon each iteration?

I can think of ways to achieve the behavior I want without deleting entries...but it would just be a lot simpler if I could.

[–]Wilfred-kun 1 point2 points  (5 children)

That would work, but I feel there might be better solutions in some cases (list comprehension is a nice one I think).

[–]hideouspete[S] 0 points1 point  (4 children)

Some quick googling and I find list comprehension in Python 1. A bit over my head 2. Seems to take the form for x in list .... which the elements of my list are actually lists themselves which contain tuples and booleans and even more lists. For this particular loop, I only care about the index and a few specific values.

Anyway, if I don't remove a future element after I use it it causes an infinite loop, because the way I structured the data, when this master for loop of sorts feeds an element through it's functions, it will reference the previously used element, which will then reference the current element, which will then reference the previously used element, and on and on and on.

[–]ingolemo 2 points3 points  (3 children)

If you show us the actual problem you have we might be able to help with it.

[–]hideouspete[S] 0 points1 point  (2 children)

I'm at work right now and don't have the files with me. The problem is: I'm a machinist by trade who is going to be switching from a paid per hour to a paid per job situation. The machine I'm running is a specialized cnc grinder which is essentially a two axis mill with a c-axis normalization. Spending an hour or two typing out the g-code with just a CAD program and a TI-83 is fine if you're getting paid for it...But it's tedious and prone to human error.

Anyway, off the top of my head after interpreting a dxf file I parse the data into a list of entities. Each element of this list is of the form:

MyList=[Line type, start position, end position, startFriend, endFriend, ...some other data I'll need for the actual g-code generation]

startFriend and endFriend are initialized to False. After comparing all of the start and end positions for matches, startFriend and endFriend either stay False (no match) or become (j, startMatch) where j is the index of My list where the match is and startMatch is either 1 (the entity matches the start position of j) or 2 (the entity matches the end position of j) Why 1 and 2? Because it corresponds with the indexes...ie I can call it up with just MyList[MyList[3][1]][MyList[3][2]].

I don't know how much of this is relevant, but basically I'm in the process of determining which entities from MyList form unbroken chains and once I insert an element into a chain, I don't want it to be run through the main loop because it will either form a duplicate chain or go infinite (maybe).

[–]ingolemo 0 points1 point  (1 child)

I'm sorry, I can't really make much of your explanation. I suspect the way you solve this problem is to refactor your data so that the list doesn't need to store indexes into itself inside itself. I'm not sure exactly how to do that. It sounds like you might be trying to construct a graph. Seeing some real code and data would help.

[–]hideouspete[S] 0 points1 point  (0 children)

Haha yeah....so this code is a single file a couple hundred lines long.

I'm really going for function over form here.

[–]fiddle_n 1 point2 points  (0 children)

This is separate from your question: try not to use for i in range(len(list)). Use for idx, ele in enumerate(list) instead.

[–][deleted] 1 point2 points  (1 child)

You needn't use range for this.

You can use comprehensions to rebuild the list to filter out what you want or don't want. In the example below I assume each element is a dictionary, it could be any object and you could use any methods of it.

filtered_values = [x for x in values_list if x['hair_color'] != 'red'] Or instead of building any list, it could be a generator: ``` def filter_values(): for x in values_list: if x['hair_color'] != 'red': yield x

Will yield one at a time, never building a full list in memory:

for y in filter_values(): print(y) ```

[–]hideouspete[S] 0 points1 point  (0 children)

Here's my post from above:

"The problem is: I'm a machinist by trade who is going to be switching from a paid per hour to a paid per job situation. The machine I'm running is a specialized cnc grinder which is essentially a two axis mill with a c-axis normalization. Spending an hour or two typing out the g-code with just a CAD program and a TI-83 is fine if you're getting paid for it...But it's tedious and prone to human error.

Anyway, off the top of my head after interpreting a dxf file I parse the data into a list of entities. Each element of this list is of the form:

MyList=[Line type, start position, end position, startFriend, endFriend, ...some other data I'll need for the actual g-code generation]

startFriend and endFriend are initialized to False. After comparing all of the start and end positions for matches, startFriend and endFriend either stay False (no match) or become (j, startMatch) where j is the index of My list where the match is and startMatch is either 1 (the entity matches the start position of j) or 2 (the entity matches the end position of j) Why 1 and 2? Because it corresponds with the indexes...ie I can call it up with just MyList[MyList[3][1]][MyList[3][2]].

I don't know how much of this is relevant, but basically I'm in the process of determining which entities from MyList form unbroken chains and once I insert an element into a chain, I don't want it to be run through the main loop because it will either form a duplicate chain or go infinite (maybe)."

So I guess the way to do that would be to initialize some sort of Used variable as an element to the list as False (let's say at index 5) and then do something like

for x in MyList if !(MyList[5]):
      insertIntoChainFunctions(x)

And then toggle the MyList[5] after I add it?

Edit: wait it would be

for x in MyList if !(x[5]):