3

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.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Nekoso
  • 113
  • 5

7 Answers7

11

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]
Andrii Rusanov
  • 4,405
  • 2
  • 34
  • 54
  • 3
    Why not just call max on the group? – Padraic Cunningham Feb 25 '16 at 09:03
  • 2
    I've been seeing [**`itertools.groupby`**](https://docs.python.org/2/library/itertools.html#itertools.groupby) used in a lot of answers recently. I think it's about time I understood it (c:. There are a few good answers to this question: [How do I use Python's itertools.groupby()?](https://stackoverflow.com/questions/773/how-do-i-use-pythons-itertools-groupby) – Peter Wood Feb 25 '16 at 09:19
  • @PeterWood agree, but actually whole itertools library is really handy, not only groupby. Personally I use chain quite often – Andrii Rusanov Feb 25 '16 at 09:20
  • @AndreyRusanov I use **`itertools`** all the time, but I've had a blind spot/fear for **`groupby`**. – Peter Wood Feb 25 '16 at 09:22
2
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)])
Avión
  • 7,963
  • 11
  • 64
  • 105
aghast
  • 14,785
  • 3
  • 24
  • 56
2

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]
DevShark
  • 8,558
  • 9
  • 32
  • 56
1

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.

IanS
  • 15,771
  • 9
  • 60
  • 84
1

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]
Community
  • 1
  • 1
Forge
  • 6,538
  • 6
  • 44
  • 64
0

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)
JRazor
  • 2,707
  • 18
  • 27
0
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
yael
  • 307
  • 1
  • 7