0

Hi I am trying to figure out this problem I'm having where I for example

I enter "123" I get [1,2,3] but the problem is when I enter "1-23" I want to get [1,-2,3]

I cant figure out how to make it so that when it recognizes there is a negative it would make the next one a negative (by multiplying it by one)

if I delete the first for it only works for positives

heres my code (sorry I'm a python beginner)

listy = []
for i in a:
    i + 1 = i * -1
for i in a:
    listy.append(i)
Sam
  • 75
  • 1
  • 5
  • `i + 1 = i * -1` is illegal in Python. Please add a complete example. – DYZ Nov 26 '18 at 08:18
  • How do you obtain or define `a`? I see the answers assume it's an input string, but I want to clarify. – Ken Y-N Nov 26 '18 at 08:24
  • 1
    @DYZ: OP says they are a beginner; this could well be the complete example, to the extent of their ability. Be kind :) We require people to be proactive and try their best, not necessarily to be correct. – Amadan Nov 26 '18 at 08:24
  • https://stackoverflow.com/questions/14785495/how-to-change-index-of-for-loop-in-python <- duplicate? – timgeb Nov 26 '18 at 08:27
  • @timgeb: No, the question is not about skipping, but modifiying a future element of the iterator (which is an XY question). – Amadan Nov 26 '18 at 08:28
  • @Amadan got it! – timgeb Nov 26 '18 at 08:29

5 Answers5

5

Regular expressions can be used to extract single digits ("\d"), optionally preceeded by a minus sign ("-?"):

import re
[int(x) for x in re.findall("-?\d", "1-23")]
#[1, -2, 3]
[int(x) for x in re.findall("-?\d", "123")]
#[1, 2, 3]
DYZ
  • 55,249
  • 10
  • 64
  • 93
1

You can't do it the way you imagine. One way is to remember the sign, and apply it when it matters:

a = "1-23"
listy = []
sign = 1
for i in a:
    if i == "-":
        sign = -1
    else:
        listy.append(int(i) * sign)
        sign = 1

The other is to use regexp to help you parse the string EDIT: as demonstrated beautifully in DYZ's answer :)

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

Set a conditional flag to keep track of whether the previous character was a '-':

a = "1-23"

listy = []

negative = False
for i in a:
    if i == '-':
        negative = True
        continue
    if negative == True:
        listy.append((int(i)*-1))
    else:
        listy.append(int(i))
    negative = False

print(listy)
ntg
  • 12,950
  • 7
  • 74
  • 95
Jay
  • 24,173
  • 25
  • 93
  • 141
0

You can use a flag to check for negative numbers.

listy = []
a = input("numbers: ")
neg_flag = False

for i in a:
    if i == '-':
        neg_flag = True
    else:
        if neg_flag:
            listy.append("-"+i)
            neg_flag = False
    else:
        listy.append(i)

print(listy)
bunbun
  • 2,595
  • 3
  • 34
  • 52
0

Assuming you don't want a regex solution, here's a solution using iter()

s='1-23'
res = []

iterator = iter(range(len(s)))
for i in iterator:
    if s[i] == '-':
        res.append(-1*int(s[i+1]))
        next(iterator, None)
    else:
        res.append(int(s[i]))

print(res)
# [1, -2, 3]

By using s[i+1] we're going to the 'next' element in the array. And by using next(iterator, None) we're skipping 1 element in the array

Also, characters are transformed into ints. you can cast them back to str if you'd like.

Eran Moshe
  • 3,062
  • 2
  • 22
  • 41