The snippet of code that’s messing up:
equ2 = ['+', '10', '+', '2', '-', '2', '+', '4']
flag4 = [ ]
for k in equ2[:]:
if equ2[equ2.index(k)-1] == '+':
del(equ2[equ2.index(k)])
flag4.append('-' + k)
elif equ2[equ2.index(k)-1] == '-':
del(equ2[equ2.index(k)])
flag4.append('+' + k)
print flag4
*Edit: I messed up the variables, sorry about that 😡
*Edit2: Sorry again D: jedwards, that is the output I’m getting, I got the other output by deleting from the copied list
Output I’m getting:
['-10', '-2', '--', '-2', '-4']
Output I want:
['-10', '-2', '+'2, '-4']
I figured out that when it got to the ‘-‘, it checks the original list (now modified to + – 2 + 4), and sees that the item before the ‘-‘ is a ‘+’, so it appends ‘–‘, then checks the original list again (now + 2 + 4) and sees the ‘+’ before the 2, so it appends ‘-2’ (fixes itself sorta after that). Is it checking the original list the whole time?
I’m not really conviced this is the best way to do this, the following works
Which outputs
Edit: Alternatively, using list comprehensions and not modifying
equ2:Which also outputs