How to refer to the next reference in a for loop?












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)









share|improve this question























  • i + 1 = i * -1 is illegal in Python. Please add a complete example.

    – DYZ
    Nov 26 '18 at 8: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 8: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 8:24













  • stackoverflow.com/questions/14785495/… <- duplicate?

    – timgeb
    Nov 26 '18 at 8: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 8:28


















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)









share|improve this question























  • i + 1 = i * -1 is illegal in Python. Please add a complete example.

    – DYZ
    Nov 26 '18 at 8: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 8: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 8:24













  • stackoverflow.com/questions/14785495/… <- duplicate?

    – timgeb
    Nov 26 '18 at 8: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 8:28
















0












0








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)









share|improve this question














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)






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 8:17









SamSam

334




334













  • i + 1 = i * -1 is illegal in Python. Please add a complete example.

    – DYZ
    Nov 26 '18 at 8: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 8: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 8:24













  • stackoverflow.com/questions/14785495/… <- duplicate?

    – timgeb
    Nov 26 '18 at 8: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 8:28





















  • i + 1 = i * -1 is illegal in Python. Please add a complete example.

    – DYZ
    Nov 26 '18 at 8: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 8: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 8:24













  • stackoverflow.com/questions/14785495/… <- duplicate?

    – timgeb
    Nov 26 '18 at 8: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 8:28



















i + 1 = i * -1 is illegal in Python. Please add a complete example.

– DYZ
Nov 26 '18 at 8:18







i + 1 = i * -1 is illegal in Python. Please add a complete example.

– DYZ
Nov 26 '18 at 8: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 8:24







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 8:24






1




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 8:24







@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 8:24















stackoverflow.com/questions/14785495/… <- duplicate?

– timgeb
Nov 26 '18 at 8:27





stackoverflow.com/questions/14785495/… <- duplicate?

– timgeb
Nov 26 '18 at 8: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 8:28







@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 8:28














5 Answers
5






active

oldest

votes


















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 :)






share|improve this answer
























  • Thank you, this works for me!

    – Sam
    Nov 26 '18 at 15:40



















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]





share|improve this answer

































    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)





    share|improve this answer































      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.






      share|improve this answer

































        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)





        share|improve this answer

























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477024%2fhow-to-refer-to-the-next-reference-in-a-for-loop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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 :)






          share|improve this answer
























          • Thank you, this works for me!

            – Sam
            Nov 26 '18 at 15:40
















          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 :)






          share|improve this answer
























          • Thank you, this works for me!

            – Sam
            Nov 26 '18 at 15:40














          1












          1








          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 :)






          share|improve this answer













          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 :)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 8:23









          AmadanAmadan

          133k13147198




          133k13147198













          • Thank you, this works for me!

            – Sam
            Nov 26 '18 at 15:40



















          • Thank you, this works for me!

            – Sam
            Nov 26 '18 at 15:40

















          Thank you, this works for me!

          – Sam
          Nov 26 '18 at 15:40





          Thank you, this works for me!

          – Sam
          Nov 26 '18 at 15:40













          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]





          share|improve this answer






























            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]





            share|improve this answer




























              5












              5








              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]





              share|improve this answer















              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]






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 26 '18 at 8:29

























              answered Nov 26 '18 at 8:22









              DYZDYZ

              28k62150




              28k62150























                  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)





                  share|improve this answer




























                    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)





                    share|improve this answer


























                      0












                      0








                      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)





                      share|improve this answer













                      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)






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 26 '18 at 8:25









                      bunbunbunbun

                      2,08532449




                      2,08532449























                          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.






                          share|improve this answer






























                            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.






                            share|improve this answer




























                              0












                              0








                              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.






                              share|improve this answer















                              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.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 26 '18 at 8:45

























                              answered Nov 26 '18 at 8:31









                              Eran MosheEran Moshe

                              1,408723




                              1,408723























                                  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)





                                  share|improve this answer






























                                    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)





                                    share|improve this answer




























                                      0












                                      0








                                      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)





                                      share|improve this answer















                                      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)






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 26 '18 at 9:00









                                      ntg

                                      4,51643353




                                      4,51643353










                                      answered Nov 26 '18 at 8:23









                                      JayJay

                                      15.3k2165122




                                      15.3k2165122






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477024%2fhow-to-refer-to-the-next-reference-in-a-for-loop%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Costa Masnaga

                                          Fotorealismo

                                          Sidney Franklin