简体   繁体   中英

python :return the maximum of the first n elements in a list

I have a list and need to find two lists in list a , keeping track of the maximum/minimum, respectively.

Is there a function in some package or numpy that doesn't require loop? I need to speed up my code as my dataset is huge.

a=[4,2,6,5,2,6,9,7,10,1,2,1]
b=[];c=[];
for i in range(len(a)):
    if i==0:
        b.append(a[i])
    elif a[i]>b[-1]:
        b.append(a[i])
for i in range(len(a)):
   if i==0:
       c.append(a[i])
   elif a[i]<c[-1]:
       c.append(a[i])
#The output should be a list :
b=[4,6,9,10];c=[4,2,1] 

Since you are saying you are dealing with a very large dataset, and want to avoid using loops, maybe this is a potential solution, which keeps the loops to a minimum:

def while_loop(a):
    b = [a[0]]
    c = [a[0]]
    a = np.array(a[1:])
    while a.size:
        if a[0] > b[-1]:
            b.append(a[0])
        elif a[0] < c[-1]:
            c.append(a[0])
        a = a[(a > b[-1]) | (a < c[-1])]

    return b, c

EDIT:

def for_loop(a):
    b = [a[0]]
    c = [a[0]]
    for x in a[1:]:
        if x > b[-1]:
            b.append(x)
        elif x < c[-1]:
            c.append(x)

    return b, c


print(
    timeit(lambda: while_loop(np.random.randint(0, 10000, 10000)), number=100000)
)  # 27.847886939000002
print(
    timeit(lambda: for_loop(np.random.randint(0, 10000, 10000)), number=100000)
)  # 112.90950811199998

Ok, so I just checked the timing against the regular for loop, and the while loop seems to be about 4-5x faster. No guarantee though, since this strongly seems to depend on the structure of your dataset (see comments).

To start, you can simply initialize b and c with the first element of a . This simplifies the loop (of which you only need 1):

a = [...]
b = [a[0]]
c = [a[0]]
for x in a[1:]:
    if x > b[-1]:
        b.append(x)
    elif x < c[-1]:
        c.append(x)

Note that inside the loop, a value of x cannot be both larger than the current maximum and smaller than the current minimum, hence the elif rather than two separate if statements.

Another optimization would be two use additional variables to avoid indexing b and c repeatedly, as well as an explicit iterator to avoid making a shallow copy of a .

a = [...]

b = []
c = []
for x in :
    if x > :
        b.append(x)
        
    elif x curr_min:
        c.append(x)
        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM