Nekoso
Nekoso

Reputation: 113

Give me highest element

I got a list like these:

List1: [1, 5, 9, 1, 5, 9, 15, 21, 29, 1, 5, 9, 15]

I want a new list, which should content the highest number, before it starts again with 1.

List_new: [9, 29, 15]

I tried this:

List_new = []
for i in range(len(List1)):
    j = List1[i]
    if j + 1 == '1': 
        List_new += [j]
    else:
        continue
print(j)

But I got an empty list back.

Upvotes: 3

Views: 148

Answers (7)

Forge
Forge

Reputation: 6834

This problem can be implemented in a one liner using python modules as in the very elegant solution suggested by Andrey. However, if you would like to follow on the logic, check out this solution.

def max_values_between_ones(numbers):     
    max_values = []
    max_value = None 
    for i in range(len(numbers)):
        if numbers[i] == 1:
            if max_value != None:
                max_values.append(max_value)
                max_value = None 
            # max_value is None when they were no values != 1 before this 1
        else:
            if max_value != None:
                # this part was missing in your code, to get the max value
                # you should be comparing the current value with the max value so far 
                max_value = max(numbers[i], max_value) 
            else:
                # set max_value to any not 1 value
                max_value = numbers[i]
    # if the list didn't end with 1, add the last max_value
    if max_value != None:
        max_values.append(max_value)
    return max_values

numbers = [1, 5, 9, 1, 5, 9, 15, 21, 29, 1, 5, 9, 15]
max_values = max_values_between_ones(numbers)
print(max_values)
>> [9, 29, 15]

Upvotes: 1

Andrii Rusanov
Andrii Rusanov

Reputation: 4606

Simply with built-in only libs:

from itertools import groupby
result = [max(group) for r, group in groupby(your_list, lambda x: x == 1) if not r]

Upvotes: 11

DevShark
DevShark

Reputation: 9112

Your code has a few issues. Here's a version that works.

list1 = [1, 5, 9, 1, 5, 9, 15, 21, 29, 1, 5, 9, 15]
list2 = []
for i in range(len(list1)-1):
   if list1[i+1] == 1:
        list2.append(list1[i])
list2.append(list1[-1]) # adds the last element

This outputs:

>>> list2
[9, 29, 15]

Upvotes: 2

yael
yael

Reputation: 337

List1 = [1, 5, 9, 1, 5, 9, 15, 21, 29, 1, 5, 9, 15]
maxi = 0
List2 = []
for i in range(0,len(List1)):
    if maxi < List1[i]:
        maxi = List1[i]
    if (i == len(List1)-1 or List1[i] == 1) and maxi > 1:
        List2.append(maxi)
        maxi = 0

print List2

Upvotes: 0

JRazor
JRazor

Reputation: 2817

Like this:

l = [1, 5, 9, 1, 5, 9, 15, 21, 29, 1, 5, 9, 15]

pos = [item for item in range(0, len(l)) if l[item] == 1]

new_list = []
for n in range(len(pos)):
    if n != len(pos) - 1:
        new_list.append(l[pos[n]:pos[n+1]])
    else:
        new_list.append(l[pos[n]:])

print map(lambda x: max(x), new_list)

Upvotes: 0

aghast
aghast

Reputation: 15300

def max_of_sublists(megalist):
    maxitem = 0
    for item in megalist:
        if item == 1 and maxitem:
            yield maxitem
            maxitem = 0
        if maxitem < item:
            maxitem = item
    yield maxitem

biglist=[1, 5, 9, 1, 5, 9, 15, 21, 29, 1, 5, 9, 15]
print([x for x in max_of_sublists(biglist)])

Upvotes: 2

IanS
IanS

Reputation: 16241

Here is a simple for loop that will answer your question:

List_new = [List1[0]]       # initialize with first element
for i in List1[1:]:         # simply iterate over list elements, not indices
    if i != 1 and i > List_new[-1]:
        List_new[-1] = i    # current element is the new maximum
    elif i == 1:
        List_new.append(i)  # encountered a 1, start looking for new maximum

See inline comments for explanations.

Upvotes: 1

Related Questions