How to check if the input is within the first column of the matrix











up vote
1
down vote

favorite
1












I have created a matrix called rix.



rix = [[1,2,3],[4,5,6][7,8,9]]


And as an input I have variable put which is a single list of length 2 which represents the position of the matrix. The part that I'm struggling in, is how do I check that the variable put represents one of the positions in the first column?










share|improve this question




























    up vote
    1
    down vote

    favorite
    1












    I have created a matrix called rix.



    rix = [[1,2,3],[4,5,6][7,8,9]]


    And as an input I have variable put which is a single list of length 2 which represents the position of the matrix. The part that I'm struggling in, is how do I check that the variable put represents one of the positions in the first column?










    share|improve this question


























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I have created a matrix called rix.



      rix = [[1,2,3],[4,5,6][7,8,9]]


      And as an input I have variable put which is a single list of length 2 which represents the position of the matrix. The part that I'm struggling in, is how do I check that the variable put represents one of the positions in the first column?










      share|improve this question















      I have created a matrix called rix.



      rix = [[1,2,3],[4,5,6][7,8,9]]


      And as an input I have variable put which is a single list of length 2 which represents the position of the matrix. The part that I'm struggling in, is how do I check that the variable put represents one of the positions in the first column?







      python matrix






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 15:43

























      asked Nov 11 at 19:25









      John.Doe

      144




      144
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote













          Design your function to be generic; it should take the argument row_n (row number) that represents which row you want to check its bounds. Check if the row_n exists in your rix's list. Then, do a comparison between your put's second element (that represents the expected row length) with the original row_n length in the rix list.



          According to your case, you should compare if the put's second element is less than or equal to the rix's row_n length and then return your booleans.






          share|improve this answer




























            up vote
            0
            down vote













            You use len(rix) to evaluate the number of rows
            and len(rix[0]) to evaluate the number of columns your matrix has



            so



            if len(rix) <= put[0]:
            return false
            elif len(rix[0]) <= put[1]:
            return false
            else:
            return true





            share|improve this answer






























              up vote
              0
              down vote













              You also have to consider negative indices.



              def in_first_col(put, list2D):
              row_idx, col_idx = put

              # return False if we can't get the row
              try:
              row = list2D[row_idx]
              except IndexError:
              return False

              # return False if row is empty
              # return False if col_idx does not refer to first element of row
              return row and (col_idx == 0 or col_idx == -len(row))


              Demo:



              >>> rix = [[1,2,3],[4,5,6],[7,8,9]]
              >>> in_first_col([1,1], rix)
              >>> False
              >>> in_first_col([2,0], rix)
              >>> True
              >>> in_first_col([5,0], rix)
              >>> False
              >>> in_first_col([-2,-3], rix)
              >>> True





              share|improve this answer





















              • (Also works for rows of different lengths.)
                – timgeb
                Nov 11 at 19:53












              • Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
                – Andrew Naguib
                Nov 11 at 21:07











              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%2f53252360%2fhow-to-check-if-the-input-is-within-the-first-column-of-the-matrix%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
              0
              down vote













              Design your function to be generic; it should take the argument row_n (row number) that represents which row you want to check its bounds. Check if the row_n exists in your rix's list. Then, do a comparison between your put's second element (that represents the expected row length) with the original row_n length in the rix list.



              According to your case, you should compare if the put's second element is less than or equal to the rix's row_n length and then return your booleans.






              share|improve this answer

























                up vote
                0
                down vote













                Design your function to be generic; it should take the argument row_n (row number) that represents which row you want to check its bounds. Check if the row_n exists in your rix's list. Then, do a comparison between your put's second element (that represents the expected row length) with the original row_n length in the rix list.



                According to your case, you should compare if the put's second element is less than or equal to the rix's row_n length and then return your booleans.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Design your function to be generic; it should take the argument row_n (row number) that represents which row you want to check its bounds. Check if the row_n exists in your rix's list. Then, do a comparison between your put's second element (that represents the expected row length) with the original row_n length in the rix list.



                  According to your case, you should compare if the put's second element is less than or equal to the rix's row_n length and then return your booleans.






                  share|improve this answer












                  Design your function to be generic; it should take the argument row_n (row number) that represents which row you want to check its bounds. Check if the row_n exists in your rix's list. Then, do a comparison between your put's second element (that represents the expected row length) with the original row_n length in the rix list.



                  According to your case, you should compare if the put's second element is less than or equal to the rix's row_n length and then return your booleans.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 19:35









                  Andrew Naguib

                  1,2361420




                  1,2361420
























                      up vote
                      0
                      down vote













                      You use len(rix) to evaluate the number of rows
                      and len(rix[0]) to evaluate the number of columns your matrix has



                      so



                      if len(rix) <= put[0]:
                      return false
                      elif len(rix[0]) <= put[1]:
                      return false
                      else:
                      return true





                      share|improve this answer



























                        up vote
                        0
                        down vote













                        You use len(rix) to evaluate the number of rows
                        and len(rix[0]) to evaluate the number of columns your matrix has



                        so



                        if len(rix) <= put[0]:
                        return false
                        elif len(rix[0]) <= put[1]:
                        return false
                        else:
                        return true





                        share|improve this answer

























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You use len(rix) to evaluate the number of rows
                          and len(rix[0]) to evaluate the number of columns your matrix has



                          so



                          if len(rix) <= put[0]:
                          return false
                          elif len(rix[0]) <= put[1]:
                          return false
                          else:
                          return true





                          share|improve this answer














                          You use len(rix) to evaluate the number of rows
                          and len(rix[0]) to evaluate the number of columns your matrix has



                          so



                          if len(rix) <= put[0]:
                          return false
                          elif len(rix[0]) <= put[1]:
                          return false
                          else:
                          return true






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 11 at 19:44

























                          answered Nov 11 at 19:39









                          Alejandro Barrera Lozano

                          11




                          11






















                              up vote
                              0
                              down vote













                              You also have to consider negative indices.



                              def in_first_col(put, list2D):
                              row_idx, col_idx = put

                              # return False if we can't get the row
                              try:
                              row = list2D[row_idx]
                              except IndexError:
                              return False

                              # return False if row is empty
                              # return False if col_idx does not refer to first element of row
                              return row and (col_idx == 0 or col_idx == -len(row))


                              Demo:



                              >>> rix = [[1,2,3],[4,5,6],[7,8,9]]
                              >>> in_first_col([1,1], rix)
                              >>> False
                              >>> in_first_col([2,0], rix)
                              >>> True
                              >>> in_first_col([5,0], rix)
                              >>> False
                              >>> in_first_col([-2,-3], rix)
                              >>> True





                              share|improve this answer





















                              • (Also works for rows of different lengths.)
                                – timgeb
                                Nov 11 at 19:53












                              • Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
                                – Andrew Naguib
                                Nov 11 at 21:07















                              up vote
                              0
                              down vote













                              You also have to consider negative indices.



                              def in_first_col(put, list2D):
                              row_idx, col_idx = put

                              # return False if we can't get the row
                              try:
                              row = list2D[row_idx]
                              except IndexError:
                              return False

                              # return False if row is empty
                              # return False if col_idx does not refer to first element of row
                              return row and (col_idx == 0 or col_idx == -len(row))


                              Demo:



                              >>> rix = [[1,2,3],[4,5,6],[7,8,9]]
                              >>> in_first_col([1,1], rix)
                              >>> False
                              >>> in_first_col([2,0], rix)
                              >>> True
                              >>> in_first_col([5,0], rix)
                              >>> False
                              >>> in_first_col([-2,-3], rix)
                              >>> True





                              share|improve this answer





















                              • (Also works for rows of different lengths.)
                                – timgeb
                                Nov 11 at 19:53












                              • Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
                                – Andrew Naguib
                                Nov 11 at 21:07













                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              You also have to consider negative indices.



                              def in_first_col(put, list2D):
                              row_idx, col_idx = put

                              # return False if we can't get the row
                              try:
                              row = list2D[row_idx]
                              except IndexError:
                              return False

                              # return False if row is empty
                              # return False if col_idx does not refer to first element of row
                              return row and (col_idx == 0 or col_idx == -len(row))


                              Demo:



                              >>> rix = [[1,2,3],[4,5,6],[7,8,9]]
                              >>> in_first_col([1,1], rix)
                              >>> False
                              >>> in_first_col([2,0], rix)
                              >>> True
                              >>> in_first_col([5,0], rix)
                              >>> False
                              >>> in_first_col([-2,-3], rix)
                              >>> True





                              share|improve this answer












                              You also have to consider negative indices.



                              def in_first_col(put, list2D):
                              row_idx, col_idx = put

                              # return False if we can't get the row
                              try:
                              row = list2D[row_idx]
                              except IndexError:
                              return False

                              # return False if row is empty
                              # return False if col_idx does not refer to first element of row
                              return row and (col_idx == 0 or col_idx == -len(row))


                              Demo:



                              >>> rix = [[1,2,3],[4,5,6],[7,8,9]]
                              >>> in_first_col([1,1], rix)
                              >>> False
                              >>> in_first_col([2,0], rix)
                              >>> True
                              >>> in_first_col([5,0], rix)
                              >>> False
                              >>> in_first_col([-2,-3], rix)
                              >>> True






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 11 at 19:47









                              timgeb

                              47.4k116287




                              47.4k116287












                              • (Also works for rows of different lengths.)
                                – timgeb
                                Nov 11 at 19:53












                              • Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
                                – Andrew Naguib
                                Nov 11 at 21:07


















                              • (Also works for rows of different lengths.)
                                – timgeb
                                Nov 11 at 19:53












                              • Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
                                – Andrew Naguib
                                Nov 11 at 21:07
















                              (Also works for rows of different lengths.)
                              – timgeb
                              Nov 11 at 19:53






                              (Also works for rows of different lengths.)
                              – timgeb
                              Nov 11 at 19:53














                              Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
                              – Andrew Naguib
                              Nov 11 at 21:07




                              Don't you think that posting code while showing no attempt from the OP is against the main use of SO?
                              – Andrew Naguib
                              Nov 11 at 21:07


















                              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%2f53252360%2fhow-to-check-if-the-input-is-within-the-first-column-of-the-matrix%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

                              Bressuire

                              Vorschmack

                              Quarantine