简体   繁体   中英

Finding min, max, and avg of every N-values in a list

I have just started learning Python for data Analytics and I found a simple but unsolvable for me problem. I want to find min, max and average of every N-numbers in a list. And I am stuck on the best and fastest way to do it. Do I have to split the list on sublists and then join the output or is there a faster way?

For example: I have a list of

lst = [6, 6, 5, 6, 7, 11, 10, 9, 9, 8, 13, 13, 13, 13, 14]

and I want to find the min, max and avg of every 5 numbers and append them on seperatily lists like:

avg = [6.0, 9.4, 13.2]
min = [5.0, 8.0, 13.0]
max = [7.0, 11.0, 14.0]

If i understand you correctly:

Here is an example

lst = [6, 6, 5, 6, 7, 11, 10, 9, 9, 8, 13, 13, 13, 13, 14]

def Average(lst):
    return sum(lst) / len(lst)

new = [Average(lst[i:i+5]) for i in range(0, len(lst), 5)]

UPDATE: for all:

[[Average(lst[i:i+5]) for i in range(0, len(lst), 5)], [max(lst[i:i+5]) for i in range(0, len(lst), 5)], [min(lst[i:i+5]) for i in range(0, len(lst), 5)]]

however write them seperately;)

Here is a simple solution for the example you gave:

lst = [6, 6, 5, 6, 7, 11, 10, 9, 9, 8, 13, 13, 13, 13, 14]

lavg = [sum(lst[i:i+5])/len((lst[i:i+5])) for i in range(0, len(lst), 5)]
lmin = [min(lst[i:i+5]) for i in range(0, len(lst), 5)]
lmax = [max(lst[i:i+5]) for i in range(0, len(lst), 5)]

print(lavg)
print(lmin)
print(lmax)

output is:

[6.0, 9.4, 13.2]
[5, 8, 13]
[7, 11, 14]

I'd do it this way: find out the amount of "slices" your list contains, round it up to the bigger number, and then run a loop in range of this value, using incremented variable to assess the indexes of each slice. And within each iteration of the loop, apply the functions you need - max() , min() or whatever.

If needed, you can append them to the pre-created lists, or simply print them out, as you wish

lst = [6, 6, 5, 6, 7, 11, 10, 9, 9, 8, 13, 13, 13, 13, 14]
slices = round(len(lst) / 5)
slicer = 0
for i in range(slices):
    max_val = max(lst[slicer:slicer + 5])
    slicer +=5
    print(max_val)

Note: I do not generally believe in offering answers for questions that show little effort, but other answers have been offered, and this one only builds on those existing answers.


The other answers seem oriented toward iterating over groups of 5 elements for each stat you wish to accumulate.

For large datasets this will be inefficient. What if you want to generate them all on a single pass?

stats = [[], [], []]
for sublist in (lst[i:i+5] for i in range(0, len(lst), 5)):
    stats[0].append(sum(sublist) / len(sublist))
    stats[1].append(min(sublist))
    stats[2].append(max(sublist))

stats = [tuple(stat) for stat in stats]

If we further wanted to generalize this, let's call the function applyn because it applies n functions to an iterable item and returns the results as a list of tuples.

def applyn(iter, *funs):
    results = [[] for _ in funs]
    for x in iter:
        for i, f in enumerate(funs):
            results[i].append(f(x))
    return [tuple(x) for x in results]

Now, we can write:

stats = applyn((lst[i:i+5] for i in range(0, len(lst), 5)), 
               lambda x: sum(x) / len(x), 
               min, max)

And we get:

[(6.0, 9.4, 13.2), (5, 8, 13), (7, 11, 14)]

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