Python: Multiply a number in a string by a constant











up vote
2
down vote

favorite












I am trying to multiply by a constant a series of number in a string. I have something like that:



my_string = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'


I can easily do this:



my_new_string = re.sub('(?<=AMOUNT=")d+', lambda _:str(38000 *2), my_string)


This works, but that is not what I need.



What I need as an output as the following:



my_new_string = 'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'


Does anyone have an idea on how to proceed? Thank you










share|improve this question
























  • Why did you put 38000 *2 into your regexp?
    – Psytho
    Nov 19 at 14:28






  • 1




    It is certainly possible, but IMHO it would be much better to compute the values BEFORE writing the string.
    – Serge Ballesta
    Nov 19 at 14:28










  • Yes, but I am just simplifying. The actual case involves a huge string coming from an xml file in which I need to multiply by 2 the values in it.
    – WhelanG
    Nov 19 at 14:30















up vote
2
down vote

favorite












I am trying to multiply by a constant a series of number in a string. I have something like that:



my_string = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'


I can easily do this:



my_new_string = re.sub('(?<=AMOUNT=")d+', lambda _:str(38000 *2), my_string)


This works, but that is not what I need.



What I need as an output as the following:



my_new_string = 'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'


Does anyone have an idea on how to proceed? Thank you










share|improve this question
























  • Why did you put 38000 *2 into your regexp?
    – Psytho
    Nov 19 at 14:28






  • 1




    It is certainly possible, but IMHO it would be much better to compute the values BEFORE writing the string.
    – Serge Ballesta
    Nov 19 at 14:28










  • Yes, but I am just simplifying. The actual case involves a huge string coming from an xml file in which I need to multiply by 2 the values in it.
    – WhelanG
    Nov 19 at 14:30













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am trying to multiply by a constant a series of number in a string. I have something like that:



my_string = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'


I can easily do this:



my_new_string = re.sub('(?<=AMOUNT=")d+', lambda _:str(38000 *2), my_string)


This works, but that is not what I need.



What I need as an output as the following:



my_new_string = 'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'


Does anyone have an idea on how to proceed? Thank you










share|improve this question















I am trying to multiply by a constant a series of number in a string. I have something like that:



my_string = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'


I can easily do this:



my_new_string = re.sub('(?<=AMOUNT=")d+', lambda _:str(38000 *2), my_string)


This works, but that is not what I need.



What I need as an output as the following:



my_new_string = 'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'


Does anyone have an idea on how to proceed? Thank you







python string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 14:23

























asked Nov 19 at 14:21









WhelanG

687




687












  • Why did you put 38000 *2 into your regexp?
    – Psytho
    Nov 19 at 14:28






  • 1




    It is certainly possible, but IMHO it would be much better to compute the values BEFORE writing the string.
    – Serge Ballesta
    Nov 19 at 14:28










  • Yes, but I am just simplifying. The actual case involves a huge string coming from an xml file in which I need to multiply by 2 the values in it.
    – WhelanG
    Nov 19 at 14:30


















  • Why did you put 38000 *2 into your regexp?
    – Psytho
    Nov 19 at 14:28






  • 1




    It is certainly possible, but IMHO it would be much better to compute the values BEFORE writing the string.
    – Serge Ballesta
    Nov 19 at 14:28










  • Yes, but I am just simplifying. The actual case involves a huge string coming from an xml file in which I need to multiply by 2 the values in it.
    – WhelanG
    Nov 19 at 14:30
















Why did you put 38000 *2 into your regexp?
– Psytho
Nov 19 at 14:28




Why did you put 38000 *2 into your regexp?
– Psytho
Nov 19 at 14:28




1




1




It is certainly possible, but IMHO it would be much better to compute the values BEFORE writing the string.
– Serge Ballesta
Nov 19 at 14:28




It is certainly possible, but IMHO it would be much better to compute the values BEFORE writing the string.
– Serge Ballesta
Nov 19 at 14:28












Yes, but I am just simplifying. The actual case involves a huge string coming from an xml file in which I need to multiply by 2 the values in it.
– WhelanG
Nov 19 at 14:30




Yes, but I am just simplifying. The actual case involves a huge string coming from an xml file in which I need to multiply by 2 the values in it.
– WhelanG
Nov 19 at 14:30












3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










You just need to make a little modification:



re.sub('(?<=AMOUNT=")d+', lambda match: str(int(match.group()) * 2), my_string)


The object being passed into lambda is the re.Match object with number you needed. Now you just need to extract the number with .group() method, convert to int * 2 and convert back to str.






share|improve this answer





















  • Thanks a lot, this is what I was missing
    – WhelanG
    Nov 19 at 14:38


















up vote
2
down vote













You can access the matching group in the lambda, as



re.sub('(?<=AMOUNT=")d+', lambda m:str(int(m.group(0))*2), my_string)





share|improve this answer




























    up vote
    0
    down vote













    You can define a custom function and use f-strings:



    def splitter(x):
    key, val = x.split('=')
    return f'{key}="{int(val[1:-1])*2}"'

    mystr = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'

    res = ' '.join(map(splitter, mystr.split()))

    'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'





    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',
      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%2f53376627%2fpython-multiply-a-number-in-a-string-by-a-constant%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      You just need to make a little modification:



      re.sub('(?<=AMOUNT=")d+', lambda match: str(int(match.group()) * 2), my_string)


      The object being passed into lambda is the re.Match object with number you needed. Now you just need to extract the number with .group() method, convert to int * 2 and convert back to str.






      share|improve this answer





















      • Thanks a lot, this is what I was missing
        – WhelanG
        Nov 19 at 14:38















      up vote
      2
      down vote



      accepted










      You just need to make a little modification:



      re.sub('(?<=AMOUNT=")d+', lambda match: str(int(match.group()) * 2), my_string)


      The object being passed into lambda is the re.Match object with number you needed. Now you just need to extract the number with .group() method, convert to int * 2 and convert back to str.






      share|improve this answer





















      • Thanks a lot, this is what I was missing
        – WhelanG
        Nov 19 at 14:38













      up vote
      2
      down vote



      accepted







      up vote
      2
      down vote



      accepted






      You just need to make a little modification:



      re.sub('(?<=AMOUNT=")d+', lambda match: str(int(match.group()) * 2), my_string)


      The object being passed into lambda is the re.Match object with number you needed. Now you just need to extract the number with .group() method, convert to int * 2 and convert back to str.






      share|improve this answer












      You just need to make a little modification:



      re.sub('(?<=AMOUNT=")d+', lambda match: str(int(match.group()) * 2), my_string)


      The object being passed into lambda is the re.Match object with number you needed. Now you just need to extract the number with .group() method, convert to int * 2 and convert back to str.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 19 at 14:33









      Idlehands

      3,8571417




      3,8571417












      • Thanks a lot, this is what I was missing
        – WhelanG
        Nov 19 at 14:38


















      • Thanks a lot, this is what I was missing
        – WhelanG
        Nov 19 at 14:38
















      Thanks a lot, this is what I was missing
      – WhelanG
      Nov 19 at 14:38




      Thanks a lot, this is what I was missing
      – WhelanG
      Nov 19 at 14:38












      up vote
      2
      down vote













      You can access the matching group in the lambda, as



      re.sub('(?<=AMOUNT=")d+', lambda m:str(int(m.group(0))*2), my_string)





      share|improve this answer

























        up vote
        2
        down vote













        You can access the matching group in the lambda, as



        re.sub('(?<=AMOUNT=")d+', lambda m:str(int(m.group(0))*2), my_string)





        share|improve this answer























          up vote
          2
          down vote










          up vote
          2
          down vote









          You can access the matching group in the lambda, as



          re.sub('(?<=AMOUNT=")d+', lambda m:str(int(m.group(0))*2), my_string)





          share|improve this answer












          You can access the matching group in the lambda, as



          re.sub('(?<=AMOUNT=")d+', lambda m:str(int(m.group(0))*2), my_string)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 14:34









          functor

          645




          645






















              up vote
              0
              down vote













              You can define a custom function and use f-strings:



              def splitter(x):
              key, val = x.split('=')
              return f'{key}="{int(val[1:-1])*2}"'

              mystr = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'

              res = ' '.join(map(splitter, mystr.split()))

              'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'





              share|improve this answer

























                up vote
                0
                down vote













                You can define a custom function and use f-strings:



                def splitter(x):
                key, val = x.split('=')
                return f'{key}="{int(val[1:-1])*2}"'

                mystr = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'

                res = ' '.join(map(splitter, mystr.split()))

                'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You can define a custom function and use f-strings:



                  def splitter(x):
                  key, val = x.split('=')
                  return f'{key}="{int(val[1:-1])*2}"'

                  mystr = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'

                  res = ' '.join(map(splitter, mystr.split()))

                  'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'





                  share|improve this answer












                  You can define a custom function and use f-strings:



                  def splitter(x):
                  key, val = x.split('=')
                  return f'{key}="{int(val[1:-1])*2}"'

                  mystr = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'

                  res = ' '.join(map(splitter, mystr.split()))

                  'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 at 14:25









                  jpp

                  86.7k194998




                  86.7k194998






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53376627%2fpython-multiply-a-number-in-a-string-by-a-constant%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