For Loop Parsing with %












1















Just started learning Python a month ago. I'm stumped on an issue.
Trying to update a dictionary using a for loop. I would like to store the variable using the for loop's variable name as key and the variable value as the dictionary's value. I'm trying to use % to make it a one liner. Here's what I have so far:



grbl_parser_d = {
'a': 'null', # Motion Mode
'b': 'null', # Coordinate System Select
'c': 'null' # Plane Select
}

grbl_out = [GC:G0 G54 G17]

def filtergrblparser():
global grbl_parser_d
for l, r in [grbl_out.strip('').split(':')]:
for a, b, c in [r.split(' ')]:
# grbl_parser_d.update({'%': x}) % x
grbl_parser_d.update({'a': a})
grbl_parser_d.update({'b': b})
grbl_parser_d.update({'c': c})


The 'grbl_out' variable is the output from an Arduino.



Trying to use something like: grbl_parser_d.update({'%': a}) % a.name



'a.name' would be the for loop's variable name, not the value. Is this even possible? Any other suggestions and tips to clean up the code would also be greatly appreciated. Thanks!










share|improve this question





























    1















    Just started learning Python a month ago. I'm stumped on an issue.
    Trying to update a dictionary using a for loop. I would like to store the variable using the for loop's variable name as key and the variable value as the dictionary's value. I'm trying to use % to make it a one liner. Here's what I have so far:



    grbl_parser_d = {
    'a': 'null', # Motion Mode
    'b': 'null', # Coordinate System Select
    'c': 'null' # Plane Select
    }

    grbl_out = [GC:G0 G54 G17]

    def filtergrblparser():
    global grbl_parser_d
    for l, r in [grbl_out.strip('').split(':')]:
    for a, b, c in [r.split(' ')]:
    # grbl_parser_d.update({'%': x}) % x
    grbl_parser_d.update({'a': a})
    grbl_parser_d.update({'b': b})
    grbl_parser_d.update({'c': c})


    The 'grbl_out' variable is the output from an Arduino.



    Trying to use something like: grbl_parser_d.update({'%': a}) % a.name



    'a.name' would be the for loop's variable name, not the value. Is this even possible? Any other suggestions and tips to clean up the code would also be greatly appreciated. Thanks!










    share|improve this question



























      1












      1








      1








      Just started learning Python a month ago. I'm stumped on an issue.
      Trying to update a dictionary using a for loop. I would like to store the variable using the for loop's variable name as key and the variable value as the dictionary's value. I'm trying to use % to make it a one liner. Here's what I have so far:



      grbl_parser_d = {
      'a': 'null', # Motion Mode
      'b': 'null', # Coordinate System Select
      'c': 'null' # Plane Select
      }

      grbl_out = [GC:G0 G54 G17]

      def filtergrblparser():
      global grbl_parser_d
      for l, r in [grbl_out.strip('').split(':')]:
      for a, b, c in [r.split(' ')]:
      # grbl_parser_d.update({'%': x}) % x
      grbl_parser_d.update({'a': a})
      grbl_parser_d.update({'b': b})
      grbl_parser_d.update({'c': c})


      The 'grbl_out' variable is the output from an Arduino.



      Trying to use something like: grbl_parser_d.update({'%': a}) % a.name



      'a.name' would be the for loop's variable name, not the value. Is this even possible? Any other suggestions and tips to clean up the code would also be greatly appreciated. Thanks!










      share|improve this question
















      Just started learning Python a month ago. I'm stumped on an issue.
      Trying to update a dictionary using a for loop. I would like to store the variable using the for loop's variable name as key and the variable value as the dictionary's value. I'm trying to use % to make it a one liner. Here's what I have so far:



      grbl_parser_d = {
      'a': 'null', # Motion Mode
      'b': 'null', # Coordinate System Select
      'c': 'null' # Plane Select
      }

      grbl_out = [GC:G0 G54 G17]

      def filtergrblparser():
      global grbl_parser_d
      for l, r in [grbl_out.strip('').split(':')]:
      for a, b, c in [r.split(' ')]:
      # grbl_parser_d.update({'%': x}) % x
      grbl_parser_d.update({'a': a})
      grbl_parser_d.update({'b': b})
      grbl_parser_d.update({'c': c})


      The 'grbl_out' variable is the output from an Arduino.



      Trying to use something like: grbl_parser_d.update({'%': a}) % a.name



      'a.name' would be the for loop's variable name, not the value. Is this even possible? Any other suggestions and tips to clean up the code would also be greatly appreciated. Thanks!







      python parsing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 8:30









      Andrew McDowell

      1,9321417




      1,9321417










      asked Nov 25 '18 at 8:22









      LMS-SmokeLMS-Smoke

      62




      62
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You don't need loops for this, and I wouldn't try to cram it onto one line. Here's a simple function that should do what you want.



          def filtergrblparser(grbl_out):
          l, r = grbl_out.strip('').split(':')
          a, b, c = r.split(' ')
          grbl_parser_d = {
          'a': a, # Motion Mode
          'b': b, # Coordinate System Select
          'c': c # Plane Select
          }
          return grbl_parser_d

          # I'm assuming you meant this to be a string
          grbl_out = "[GC:G0 G54 G17]"

          grbl_parser_d = filtergrblparser(grbl_out)

          print(grbl_parser_d)
          # {'a': 'G0', 'b': 'G54', 'c': 'G17'}





          share|improve this answer































            0














            This is generally a bad idea, but it could be done with another for loop.



            # it's not clear why you're throwing this in a list just to iterate once over it
            l, r = grbl_out.strip('').split(':')
            a, b, c = r.split(' ')
            for k in ['a', 'b', 'c']:
            grbl_parser_d[k] = vars()[k]


            But really it looks like you're trying to do:



            grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' ')))


            Which is probably best written as:



            l, r = grbl_out.strip('').split(':')
            grbl_parser_d = dict(zip('abc', r.split(' ')))





            share|improve this answer
























            • Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.

              – LMS-Smoke
              Nov 25 '18 at 9:02













            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%2f53465804%2ffor-loop-parsing-with%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You don't need loops for this, and I wouldn't try to cram it onto one line. Here's a simple function that should do what you want.



            def filtergrblparser(grbl_out):
            l, r = grbl_out.strip('').split(':')
            a, b, c = r.split(' ')
            grbl_parser_d = {
            'a': a, # Motion Mode
            'b': b, # Coordinate System Select
            'c': c # Plane Select
            }
            return grbl_parser_d

            # I'm assuming you meant this to be a string
            grbl_out = "[GC:G0 G54 G17]"

            grbl_parser_d = filtergrblparser(grbl_out)

            print(grbl_parser_d)
            # {'a': 'G0', 'b': 'G54', 'c': 'G17'}





            share|improve this answer




























              1














              You don't need loops for this, and I wouldn't try to cram it onto one line. Here's a simple function that should do what you want.



              def filtergrblparser(grbl_out):
              l, r = grbl_out.strip('').split(':')
              a, b, c = r.split(' ')
              grbl_parser_d = {
              'a': a, # Motion Mode
              'b': b, # Coordinate System Select
              'c': c # Plane Select
              }
              return grbl_parser_d

              # I'm assuming you meant this to be a string
              grbl_out = "[GC:G0 G54 G17]"

              grbl_parser_d = filtergrblparser(grbl_out)

              print(grbl_parser_d)
              # {'a': 'G0', 'b': 'G54', 'c': 'G17'}





              share|improve this answer


























                1












                1








                1







                You don't need loops for this, and I wouldn't try to cram it onto one line. Here's a simple function that should do what you want.



                def filtergrblparser(grbl_out):
                l, r = grbl_out.strip('').split(':')
                a, b, c = r.split(' ')
                grbl_parser_d = {
                'a': a, # Motion Mode
                'b': b, # Coordinate System Select
                'c': c # Plane Select
                }
                return grbl_parser_d

                # I'm assuming you meant this to be a string
                grbl_out = "[GC:G0 G54 G17]"

                grbl_parser_d = filtergrblparser(grbl_out)

                print(grbl_parser_d)
                # {'a': 'G0', 'b': 'G54', 'c': 'G17'}





                share|improve this answer













                You don't need loops for this, and I wouldn't try to cram it onto one line. Here's a simple function that should do what you want.



                def filtergrblparser(grbl_out):
                l, r = grbl_out.strip('').split(':')
                a, b, c = r.split(' ')
                grbl_parser_d = {
                'a': a, # Motion Mode
                'b': b, # Coordinate System Select
                'c': c # Plane Select
                }
                return grbl_parser_d

                # I'm assuming you meant this to be a string
                grbl_out = "[GC:G0 G54 G17]"

                grbl_parser_d = filtergrblparser(grbl_out)

                print(grbl_parser_d)
                # {'a': 'G0', 'b': 'G54', 'c': 'G17'}






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 '18 at 8:36









                CyphaseCyphase

                8,39011627




                8,39011627

























                    0














                    This is generally a bad idea, but it could be done with another for loop.



                    # it's not clear why you're throwing this in a list just to iterate once over it
                    l, r = grbl_out.strip('').split(':')
                    a, b, c = r.split(' ')
                    for k in ['a', 'b', 'c']:
                    grbl_parser_d[k] = vars()[k]


                    But really it looks like you're trying to do:



                    grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' ')))


                    Which is probably best written as:



                    l, r = grbl_out.strip('').split(':')
                    grbl_parser_d = dict(zip('abc', r.split(' ')))





                    share|improve this answer
























                    • Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.

                      – LMS-Smoke
                      Nov 25 '18 at 9:02


















                    0














                    This is generally a bad idea, but it could be done with another for loop.



                    # it's not clear why you're throwing this in a list just to iterate once over it
                    l, r = grbl_out.strip('').split(':')
                    a, b, c = r.split(' ')
                    for k in ['a', 'b', 'c']:
                    grbl_parser_d[k] = vars()[k]


                    But really it looks like you're trying to do:



                    grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' ')))


                    Which is probably best written as:



                    l, r = grbl_out.strip('').split(':')
                    grbl_parser_d = dict(zip('abc', r.split(' ')))





                    share|improve this answer
























                    • Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.

                      – LMS-Smoke
                      Nov 25 '18 at 9:02
















                    0












                    0








                    0







                    This is generally a bad idea, but it could be done with another for loop.



                    # it's not clear why you're throwing this in a list just to iterate once over it
                    l, r = grbl_out.strip('').split(':')
                    a, b, c = r.split(' ')
                    for k in ['a', 'b', 'c']:
                    grbl_parser_d[k] = vars()[k]


                    But really it looks like you're trying to do:



                    grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' ')))


                    Which is probably best written as:



                    l, r = grbl_out.strip('').split(':')
                    grbl_parser_d = dict(zip('abc', r.split(' ')))





                    share|improve this answer













                    This is generally a bad idea, but it could be done with another for loop.



                    # it's not clear why you're throwing this in a list just to iterate once over it
                    l, r = grbl_out.strip('').split(':')
                    a, b, c = r.split(' ')
                    for k in ['a', 'b', 'c']:
                    grbl_parser_d[k] = vars()[k]


                    But really it looks like you're trying to do:



                    grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' ')))


                    Which is probably best written as:



                    l, r = grbl_out.strip('').split(':')
                    grbl_parser_d = dict(zip('abc', r.split(' ')))






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 25 '18 at 8:34









                    Adam SmithAdam Smith

                    34.6k63276




                    34.6k63276













                    • Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.

                      – LMS-Smoke
                      Nov 25 '18 at 9:02





















                    • Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.

                      – LMS-Smoke
                      Nov 25 '18 at 9:02



















                    Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.

                    – LMS-Smoke
                    Nov 25 '18 at 9:02







                    Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.

                    – LMS-Smoke
                    Nov 25 '18 at 9:02




















                    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%2f53465804%2ffor-loop-parsing-with%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

                    Create new schema in PostgreSQL using DBeaver

                    Deepest pit of an array with Javascript: test on Codility

                    Costa Masnaga