4

I'm looking for maximum absolute value out of chunked list.

For example, the list is:

[1, 2, 4, 5, 4, 5, 6, 7, 2, 6, -9, 6, 4, 2, 7, 8]

I want to find the maximum with lookahead = 4. For this case, it will return me:

[5, 7, 9, 8]

How can I do simply in Python?

for d in data[::4]:
    if count < LIMIT:
        count = count + 1

        if abs(d) > maximum_item:
            maximum_item = abs(d)
    else:
        max_array.append(maximum_item)

        if maximum_item > highest_line:
            highest_line = maximum_item

        maximum_item = 0
        count = 1

I know I can use for loop to check this. But I'm sure there is an easier way in python.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
moeseth
  • 1,855
  • 5
  • 23
  • 47

4 Answers4

5

Using standard Python:

[max(abs(x) for x in arr[i:i+4]) for i in range(0, len(arr), 4)]

This works also if the array cannot be evenly divided.

eumiro
  • 207,213
  • 34
  • 299
  • 261
  • what if the array cannot be evenly divided? – moeseth Dec 31 '15 at 08:53
  • @AlexBelyaev - sorry for not mentioning your comment. The extended numpy solution (with padding) was longer and took more time than this standard Python solution, so I removed it. Please remove your JQuery comment now. – eumiro Dec 31 '15 at 10:46
4

Map the list to abs(), then chunk the list and send it to max():

array = [1,2,4,5,4,5,6,7,2,6,-9,6,4,2,7,8]
array = [abs(item) for item in array]
# use linked question's answer to chunk
# array = [[1,2,4,5], [4,5,6,7], [2,6,9,6], [4,2,7,8]] # chunked abs()'ed list
values = [max(item) for item in array]

Result:

>>> values
[5, 7, 9, 8]
Community
  • 1
  • 1
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

Another way, is to use islice method from itertools module:

>>> from itertools import islice
>>> [max(islice(map(abs,array),i,i+4)) for i in range(0,len(array),4)]
[5, 7, 9, 8]

To break it down:

1 - map(abs, array) returns a list of all absolute values of array elemets
2 - islice(map(abs,array),i,i+4)) slices the array in chunks of four elements
3 - i in range(0,len(array),4) stepping range for islice to avoid overlapping

This can be wrapped in function as fellows:

def max_of_chunks(lst, chunk_size):
    lst = map(abs, lst)
    result = [max(islice(lst,i,i+chunk_size)) for i in range(0,len(lst),chunk_size)]
    return result
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
0

Upd: Oh, I've just seen newest comments to task and answers. I wasn't get task properly, my bad :) Let my old answer stay here for history. Max numbers from list chunks you can find in the way like that:

largest = [max(abs(x) for x in l[i:i+n]) for i in xrange(0, len(l), n)]

or

largest = [max(abs(x) for x in l[i:i+n]) for i in range(0, len(l), n)]

if you're use Python3.


Original answer just for history: If you had to choice some numbers (once) from not a big list, you shouldn't install big libraries like numpy for such simple tasks. There are a lot of techniques to do it with built-in Python tools. Here they are (something of them).

So we have some list and count of maximum different elements:

In [1]: l = [1, 2, 4, 5, 4, 5, 6, 7, 2, 6, -9, 6, 4, 2, 7, 8]
In [2]: n = 4

A. First we getting only unique numbers from source list by converting it to set. Then we creating a list consist of these unique numbers, sort it and finally get N last (greatest) elements:

In [3]: sorted(list(set(l)))[-n:]
Out[3]: [5, 6, 7, 8]

B. You can use built-in heapq module:

In [7]: import heapq

In [8]: heapq.nlargest(n, set(l))
Out[8]: [8, 7, 6, 5]

Of course you can 'wrap' A or B technique into some human-friendly function like def get_largest(seq, n): return sorted(list(set(l)))[-n:]. Yes I've ommited some details like handling IndexError. You should remember about it when you'll writing the code.

C. If your list(s) is very long and you had to do many of these operations so fast as Python can, you should use special third-party libraries like numpy or bottleneck.

Alex Belyaev
  • 1,417
  • 1
  • 11
  • 15