How to delete elements while traversing the list in processing.py?

the code as follows is about particles system, and it could run successfully. i understant the code. the elements in the list particles represent particles, and particles will continue to be generated and die gradually, that is, traverse the list and delete the dead elements.

But i think the code doesn’t meet the requestions which the book of the Nature of Code reposes. i want to know how to change the code to make it meet the requestions. I appreciate your help.i hope i clarify my question.

requestions:
1.the code could give us the next element in the list one at a time until we get to the end.
2. And if I remove elements or move them around in the list while were iterating, we won’t look at any elements twice or skip any by accident.

The code is as follows(The key points are in the last four lines):

class Particle(object):
    def __init__(self,l):
        self.acceleration = PVector(0, 0.05)
        self.velocity = PVector(random(-1, 1), random(-2, 0))
        self.location = l.copy()
        self.lifespan = 255.0
        
    def run(self):
        self.update()
        self.display()
        
    def update(self):
        self.velocity.add(self.acceleration)
        self.location.add(self.velocity)
        self.lifespan -= 2.0
        
    def display(self):
        stroke(0, self.lifespan)
        strokeWeight(2)
        fill(127, self.lifespan)
        ellipse(self.location.x, self.location.y, 12, 12)
        
    def isDead(self):
        return self.lifespan < 0.0

particles = []    
def setup():
    size(800, 200)
    smooth()
    
    
def draw():
    global particles
    background(255)
    particles.append(Particle(PVector(width/2, 50)))

    for i,p in enumerate(particles):
        p.run()
        if p.isDead():
            dead_p = particles.pop(i)                                                                                 

others(why i have the question and what i have done):

i am learning the book of The Nature of Code by myself.i love the book.
the book meets the requestion by Arraylist and iterator in java(which you could view it through Example 4.2: ArrayList of particles with Iterator).

I use python to rewrite the example code in the book(the book provides java codes,but i don’t know about java.). sometimes,it is difficult for me to write the code.so i see codes from github. i am confused about this code.

i view python documents for help:


but does copying or creating a new list take up more memory?

In addition, traversing from back to front will affect the drawing order of particles.This method is not desirable.

what should i do ?help!help!help!

2 Likes
3 Likes

Thank you for your attention.your are so nice.

With the help of my friend, I understand the second half of your content (from Here’s @warsoldier011’s excerpt adapted …… to the end). :joy:

Maybe I didn’t make it clear that the order of elements couldn’t be changed.My friend reminded me to use a linked list。

1 Like

I fail to spot any place on your code where the order of Particle objects in the particle[] list would matter.

Particle::display() method relies on property location for coordinates, not on particle[]'s index:
ellipse(self.location.x, self.location.y, 12, 12)

it was solved.thank you very very much. :smiley:

1 Like