I have a list

[7, 100, 83, 1000, 9, 100, 19] 

I need to multiply by one hundred only those numbers, after which comes the number one hundred. It should work out like this:

 [700, 83, 1000, 900, 19] 

I try to make it through this code:

  stack2 = [] i = 1 while i < len(stack): if stack[i] == 100: stack2.append(stack[i-1]*100) elif len(stack) == 2 and i == len(stack)-1: # Гдето здесь ошибка есть stack2.append(stack[i-1]) stack2.append(stack[i]) elif i == len(stack)-1: stack2.append(stack[i]) elif stack[i-1] != 100: stack2.append(stack[i-1]) i += 1 

Tell me how best to do it.

  • Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. - Kromster
  • one
    It is necessary to multiply only in pairs? What if it will be like this [7, 100, 100] ? - Sergey Gornostaev
  • I need to multiply by a hundred only those numbers, after which there is a hundred. I see a completely different thing - if in the adjacent pair the second value is 100, replace this pair with their product. Ps. These are not numbers, these are numbers. - Akina
  • one
    # There is a mistake here. This is a mistake in logic. Here the fuck check the conditions for the LAST item? after it there are no elements, that is, obviously after it there is no element with a value of 100 ... - Akina
  • one
    I do not cease to be surprised by the authors who ask the question and immediately get lost. The answer is not so necessary. - Sergey Gornostaev

4 answers 4

 from functools import reduce data = [7, 100, 83, 1000, 9, 100, 19] reduce(lambda acc, val: acc[:-1] + [acc[-1] * val] if val == 100 and len(acc) else acc + [val], data, []) 
     x = [7, 100, 83, 1000, 9, 100, 19] for value, index in zip(reversed(x[1:]), range(len(x) - 1, 0, -1)): if (value == 100): x[index - 1] *= x.pop(index) print(x) # [700, 83, 1000, 900, 19] 

      As an option:

       lst = [7, 100, 83, 1000, 9, 100, 19] lst2 = [] for i in range(len(lst)): if lst[i] == 100: continue if i != len(lst) - 1: if lst[i+1] == 100: lst2.append(lst[i] * 100) else: lst2.append(lst[i]) else: lst2.append(lst[i]) print(lst2) 

        The #slippyk option, only use enumerate:

         l = [7, 100, 83, 1000, 9, 100, 19] for i, v in enumerate(l): if v == 100: l[i-1] *= l.pop(i) print(l) # [700, 83, 1000, 900, 19]