9

For the list k1=[31.0, 72, 105.0, 581.5, 0, 0, 0], I would like to add a constant for example 100 to the first non-zero element in the reverse list. this is what I want: newk1=[0, 0, 0, 681.5, 105, 72, 31] As a beginner in Python I could not figure it out. Could you please help me. That is my code:

k1=[31.0, 72, 105.0, 581.5, 0, 0, 0]
Inverselist=[]


for i in range(len(etack1)):
    Inverselist.append(etack1[-(i+1)])
    print("Inverselist", Inverselist)
newk1=Inverselist
run_once = 0
while run_once < 1:      
    for j in  range(len(newk1)): 
        if newk1[j-1]>0: 
            newk1[j-1]=newk1[j-1]+100 
            run_once = 1 
            break
print("Newk1", newk1 )
rezzz
  • 151
  • 1
  • 1
  • 8
  • 3
    `reversed` is a useful built-in function for this. It's easier to reverse a list using that than your current method. –  Oct 30 '17 at 22:50
  • Why `j-1`? `j` already runs from 0 to `len(newk1)-1`, so you don't need a `j-1` for indexing. –  Oct 30 '17 at 22:52
  • Once you solved your problem, consider posting your code on https://codereview.stackexchange.com/ ; there's a lot of additional things that can be improved. –  Oct 30 '17 at 22:53
  • NameError: name 'etack1' is not defined – Brambor Apr 01 '20 at 21:57
  • Once `NameError` is fixed, then line `if newk1[j-1]>0:` in the first iteration of while loop will look at `j-1 = 0-1 = -1` => the last item in `Inverselist` or first item in the original list. This is something you probably don't want, you probably want to change this line to `if j>0 and newk1[j-1]>0`. – Brambor Apr 01 '20 at 22:02
  • Then your code will work ;). – Brambor Apr 01 '20 at 22:03
  • The code will also work if you replace all occurances of `j-1` by `j` as has been suggested by user707650. – Brambor Apr 01 '20 at 22:31
  • Also you can remove lines `run_once = 0`; `while run_once < 1: `; `run_once = 1 `. if all are deleted, nothing changes. – Brambor Apr 01 '20 at 22:33

6 Answers6

12

I think you're overthinking this:

First, reverse the list:

inverselist = k1[::-1]

Then, replace the first nonzero element:

for i, item in enumerate(inverselist):
    if item:
        inverselist[i] += 100
        break
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 3
    @rezi Note how this answer first **describes the steps in words**. Then after having a clear idea of each step, the words are translated into Python. This first step is often skipped by beginning programmers and is **the most important**. I cannot emphasize this enough. The most important step in programming is to understand the problem and describe a solution **in words**. – Code-Apprentice Oct 31 '17 at 00:19
  • And then, reverse it back? – Brambor Apr 01 '20 at 22:24
  • @Brambor: I don't think so, OP wanted a reversed list as the result – Tim Pietzcker Apr 02 '20 at 17:26
  • @TimPietzcker You are right, I read the language, which I interpreted differently and not the explicit "this is what I want" :D – Brambor Apr 03 '20 at 11:41
4

Just a silly way. Modifies the list instead of creating a new one.

k1.reverse()
k1[list(map(bool, k1)).index(1)] += 100
Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107
  • `.index(True)` sounds better, also an explanation of your answer is missing on a beginner's question. – Brambor Apr 01 '20 at 22:05
2

If you want to reverse, you can just do it by slicing. As below,

>>> a = [1,2,3]
>>> reverse_a = a[::-1]
>>> reverse_a
[3, 2, 1]

Once you go through the list, you just need to check when the first element is a non-zero element

k1=[31.0, 72, 105.0, 581.5, 0, 0, 0]
newk1= k1[::-1]
for i in range(len(newk1)):
    if newk1[i] != 0:
        newk1[i] += 100
        break
print("Newk1", newk1 ) #prints Newk1 [0, 0, 0, 681.5, 205.0, 172, 131.0]
yash
  • 1,357
  • 2
  • 23
  • 34
1

You can try this:

k1=[31.0, 72, 105.0, 581.5, 0, 0, 0]
new_list = []
flag = False
for i in k1[::-1]:
    if i > 0 and not flag:
        new_list.append(i+100)
        flag = True
    else:
        new_list.append(i)

Output:

[0, 0, 0, 681.5, 105.0, 72, 31.0]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • This also creates two copies of the original list. Not that it matters much for such a small data set, but you could do `for i in reversed(k1)` instead. –  Oct 30 '17 at 23:05
  • @StefanPochmann I added a more generic solution. Please see my recent edit. – Ajax1234 Oct 30 '17 at 23:07
1

Here's a solution using reversed instead of slicing with [::-1]:

items = [31.0, 72, 105.0, 581.5, 0, 0, 0]

inverse_items = []
found_non_zero = False
for item in reversed(items):
    if not found_non_zero and item:
        found_non_zero = True
        item += 100
    inverse_items.append(item)
-2
list1 = [31.0, 72, 105.0, 581.5, 0, 0, 0]
last_idx = len(list1)-1
print(last_idx)
for i in range(len(list1)):
    list1.insert(i,list1.pop(last_idx))  
print(list1)

Output :

[0, 0, 0, 581.5, 105.0, 72, 31.0]
4b0
  • 21,981
  • 30
  • 95
  • 142
  • This code just reverses the existing list without using an additional list.One thing i missed is to add 100 to the first elemet.we can use the code suggested above to do that, – vijay ananth Dec 04 '19 at 03:26
  • u pop the last element in the list and insert in the order of the list starting from index 0.This will reverse the list automatically. – vijay ananth Dec 04 '19 at 03:29