[Python] avoiding loops in python

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I understand that loops are a bad idea in python and I should avoid them.

    Well, I have several of those I want to avoid.

    I have a list of stuff named lipid:

    class bead:
    x = 0
    y = 0
    z = 0
    LID = 0
    t = 0

    class lipid:
    h = bead()
    b = bead()
    t = bead()
    LID = 0


    and I am doing the following (code below):

    1. initializing a 2d array of hs going over all the lipids and
    2. determine if they are counted as U or down
    3. adding value to the appropriate h

    How can I avoid, at least, the first loop?

    1:

    class h:
    cU = 0
    cD = 0
    hU = 0
    hD = 0
    h = 0

    for i in range(0,8):
    hs.append([])
    for j in range(0,8):
    index = (i,j)
    hn = h()
    hs.append(hn)


    2 and 3:

    for LID in lipids:
    l = lipids[LID]
    up = l.h.z > l.t.z
    X = (int)(l.b.x*8/L)
    Y = (int)(l.b.y*8/L)
    Z = (l.b.z)*0.5
    if up:
    hs[X][Y].hU += Z
    hs[X][Y].cU += 1
    else:
    hs[X][Y].hD += Z
    hs[X][Y].cD += 1

    Continue reading...

Compartilhe esta Página