Home > Software design >  Best Way To Convert to a list comprehension or optimisation
Best Way To Convert to a list comprehension or optimisation

Time:05-23

ret = []
sub_total = 0
        for peak, value in zip(list_1, list_2):
            sub_total  = value
            if sub_total >= mid_rule:
                # print(sub_total)
                ret.append(peak)         
                break

My main goal is optimisation because this code takes a lot of memory resources;

I have tried this;

ret = [peak if (sub_total :=0   value) >= mid_rule for peak, value in zip(min_idx, min_count)]

but not yielding any results; your help is much appreciated

CodePudding user response:

This may convert what you had into a list comprehension, but to better optimize your code we need to know why you are doing things as @Kenny Ostrom and @Mark mentioned.

sub_total = 0
ret = [peak for peak, value in zip(list_1, list_2) if (sub_total := sub_total   peak) >= mid_rule]

your if statement in the list comprehension seemed incorrect in your example and that you were doing 0 peak when it should be subtotal peak in your other example.

Taking @Jerome Richard's suggestion you could do:

ret = []
sub_total = 0
for peak, value in zip(list_1, list_2):
    sub_total  = value
    if sub_total >= mid_rule:
        yield peak

and can make a generator comprehension like

sub_total = 0
ret = (peak for peak, value in zip(list_1, list_2) if (sub_total := sub_total   peak) >= mid_rule)

Though it seems strange to create a list object only to use a single item before the break ~ you could remove having it as a list which would save you very little time. But again we need to know what is occurring to further optimize your code.

Source

CodePudding user response:

i'm a little late to this but probably the easiest way is a gen exp with some itertools functionality

from itertools import accumulate

ret = next(
    (peak for peak, running_total in zip(list_1, accumulate(list_2)) if running_total >= mid_rule),
    None,
)
  • Related