Home > Mobile >  How to make loops shorter, cleaner, when using many variables?
How to make loops shorter, cleaner, when using many variables?

Time:10-29

Is there a nice way to make loop look cleaner? Can I somehow make two loops below to look more clean, shorter? Is there are different way of defining all the parameters =0?

current = []
voltage = []
#parameters add shift value with every device loop
m = 0
n = 0
t = 0
u = 0
w = 0 
for device in range(0, rows*columns):
    m  = shift_horizontal_per_device
    n  = shift_vertical_per_device
    t  = shif_param_a_per_device
    u  = shif_param_b_per_device
    w  = shif_param_c_per_device
    # parameters add shift value with every pad loop
    o = 0 
    l = 0
    p = 0
    r = 0
    s = 0
    for pad in range(0, pads):
        o  = shift_horizontal_per_pad
        l  = shift_vertical_per_pad
        p  = shif_param_a_per_pad
        r  = shif_param_b_per_pad
        s  = shif_param_c_per_pad
        x = np.linspace(voltage_min,voltage_max,ids_per_pad)
        y = (lambda a,b,c,x: eval(math_fun))(a p t,b r u,c s w,x)
        x  = o m
        y  = n l
        voltage.extend(x)
        current.extend(y)

CodePudding user response:

The least convoluted way I see could be:

current = []
voltage = []
m, n, t, u, w = 0, 0, 0, 0, 0
for device in range(0, rows*columns):
    m  = shift_horizontal_per_device
    n  = shift_vertical_per_device
    t  = shif_param_a_per_device
    u  = shif_param_b_per_device
    w  = shif_param_c_per_device
    o, l, p, r, s = 0, 0, 0, 0, 0
    for pad in range(0, pads):
        o  = shift_horizontal_per_pad
        l  = shift_vertical_per_pad
        p  = shif_param_a_per_pad
        r  = shif_param_b_per_pad
        s  = shif_param_c_per_pad
        x = np.linspace(voltage_min,voltage_max,ids_per_pad)
        y = (lambda a,b,c,x: eval(math_fun))(a p t,b r u,c s w,x)
        x  = o m
        y  = n l
        voltage.extend(x)
        current.extend(y)
  • Related