I want iterate through the for loop to return three points












0















points = array

def get_nearest_point(x, y, n_points):

dist =
val =
j = (x, y)
for z in range(n_points):
for i in range(len(points)):
dist += [Distance(points[i][0],points[i][1],x,y)]
a = dist.index(min(dist))
val.append(points[a])
dist.remove(min(dist))
return val


Output :



get_nearest_point(50, 50,3)



[[54, 57]]



I want return something like [[54,57],[56,78],[78,90]]










share|improve this question




















  • 3





    Do you have a question?

    – jpp
    Jan 3 at 13:32
















0















points = array

def get_nearest_point(x, y, n_points):

dist =
val =
j = (x, y)
for z in range(n_points):
for i in range(len(points)):
dist += [Distance(points[i][0],points[i][1],x,y)]
a = dist.index(min(dist))
val.append(points[a])
dist.remove(min(dist))
return val


Output :



get_nearest_point(50, 50,3)



[[54, 57]]



I want return something like [[54,57],[56,78],[78,90]]










share|improve this question




















  • 3





    Do you have a question?

    – jpp
    Jan 3 at 13:32














0












0








0








points = array

def get_nearest_point(x, y, n_points):

dist =
val =
j = (x, y)
for z in range(n_points):
for i in range(len(points)):
dist += [Distance(points[i][0],points[i][1],x,y)]
a = dist.index(min(dist))
val.append(points[a])
dist.remove(min(dist))
return val


Output :



get_nearest_point(50, 50,3)



[[54, 57]]



I want return something like [[54,57],[56,78],[78,90]]










share|improve this question
















points = array

def get_nearest_point(x, y, n_points):

dist =
val =
j = (x, y)
for z in range(n_points):
for i in range(len(points)):
dist += [Distance(points[i][0],points[i][1],x,y)]
a = dist.index(min(dist))
val.append(points[a])
dist.remove(min(dist))
return val


Output :



get_nearest_point(50, 50,3)



[[54, 57]]



I want return something like [[54,57],[56,78],[78,90]]







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 16:01









Akshay L Aradhya

1,05811128




1,05811128










asked Jan 3 at 13:30









Lodwin MolotoLodwin Moloto

81




81








  • 3





    Do you have a question?

    – jpp
    Jan 3 at 13:32














  • 3





    Do you have a question?

    – jpp
    Jan 3 at 13:32








3




3





Do you have a question?

– jpp
Jan 3 at 13:32





Do you have a question?

– jpp
Jan 3 at 13:32












3 Answers
3






active

oldest

votes


















1














What you are trying to do is essentially find the indices of the smallest 3 elements in the given array. You can easily do this with numpy.argsort method



Anyways here is your code with numpy :



import numpy as np

def get_nearest_point(x, y, n):

dist = [Distance(point[0], point[1], x, y) for point in points]
indices = np.argsort(dist)
return [points[i] for i in indices[:n]]





share|improve this answer

































    1














    welcome to the site!



    The issue with your code as written is that the return statement is inside the loop- instead, move it outside. That way you append all three values to val before you return it.






    share|improve this answer
























    • There are still a lot more errors in his code.

      – Akshay L Aradhya
      Jan 3 at 16:14













    • Maybe so, but I still find it useful to answer the question as asked, especially for a new question asker.

      – Paul Becotte
      Jan 3 at 16:17



















    0














    Here we calculate distance for every point, sort it by distance and get first n points.



    def get_nearest_point(x, y, n_points):
    points_with_dist = # store here pairs (dist, point)
    for point in points:
    points_with_dist.append((
    Distance(point[0], point[1], x, y),
    point
    ))
    result_with_dist = sorted(points_with_dist, key=lambda struct: struct[0])[:n_points]
    result = [p[1] for p in result_with_dist] # we need only points
    return result





    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%2f54023277%2fi-want-iterate-through-the-for-loop-to-return-three-points%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









      1














      What you are trying to do is essentially find the indices of the smallest 3 elements in the given array. You can easily do this with numpy.argsort method



      Anyways here is your code with numpy :



      import numpy as np

      def get_nearest_point(x, y, n):

      dist = [Distance(point[0], point[1], x, y) for point in points]
      indices = np.argsort(dist)
      return [points[i] for i in indices[:n]]





      share|improve this answer






























        1














        What you are trying to do is essentially find the indices of the smallest 3 elements in the given array. You can easily do this with numpy.argsort method



        Anyways here is your code with numpy :



        import numpy as np

        def get_nearest_point(x, y, n):

        dist = [Distance(point[0], point[1], x, y) for point in points]
        indices = np.argsort(dist)
        return [points[i] for i in indices[:n]]





        share|improve this answer




























          1












          1








          1







          What you are trying to do is essentially find the indices of the smallest 3 elements in the given array. You can easily do this with numpy.argsort method



          Anyways here is your code with numpy :



          import numpy as np

          def get_nearest_point(x, y, n):

          dist = [Distance(point[0], point[1], x, y) for point in points]
          indices = np.argsort(dist)
          return [points[i] for i in indices[:n]]





          share|improve this answer















          What you are trying to do is essentially find the indices of the smallest 3 elements in the given array. You can easily do this with numpy.argsort method



          Anyways here is your code with numpy :



          import numpy as np

          def get_nearest_point(x, y, n):

          dist = [Distance(point[0], point[1], x, y) for point in points]
          indices = np.argsort(dist)
          return [points[i] for i in indices[:n]]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 16:16

























          answered Jan 3 at 16:07









          Akshay L AradhyaAkshay L Aradhya

          1,05811128




          1,05811128

























              1














              welcome to the site!



              The issue with your code as written is that the return statement is inside the loop- instead, move it outside. That way you append all three values to val before you return it.






              share|improve this answer
























              • There are still a lot more errors in his code.

                – Akshay L Aradhya
                Jan 3 at 16:14













              • Maybe so, but I still find it useful to answer the question as asked, especially for a new question asker.

                – Paul Becotte
                Jan 3 at 16:17
















              1














              welcome to the site!



              The issue with your code as written is that the return statement is inside the loop- instead, move it outside. That way you append all three values to val before you return it.






              share|improve this answer
























              • There are still a lot more errors in his code.

                – Akshay L Aradhya
                Jan 3 at 16:14













              • Maybe so, but I still find it useful to answer the question as asked, especially for a new question asker.

                – Paul Becotte
                Jan 3 at 16:17














              1












              1








              1







              welcome to the site!



              The issue with your code as written is that the return statement is inside the loop- instead, move it outside. That way you append all three values to val before you return it.






              share|improve this answer













              welcome to the site!



              The issue with your code as written is that the return statement is inside the loop- instead, move it outside. That way you append all three values to val before you return it.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 3 at 16:10









              Paul BecottePaul Becotte

              4,90921630




              4,90921630













              • There are still a lot more errors in his code.

                – Akshay L Aradhya
                Jan 3 at 16:14













              • Maybe so, but I still find it useful to answer the question as asked, especially for a new question asker.

                – Paul Becotte
                Jan 3 at 16:17



















              • There are still a lot more errors in his code.

                – Akshay L Aradhya
                Jan 3 at 16:14













              • Maybe so, but I still find it useful to answer the question as asked, especially for a new question asker.

                – Paul Becotte
                Jan 3 at 16:17

















              There are still a lot more errors in his code.

              – Akshay L Aradhya
              Jan 3 at 16:14







              There are still a lot more errors in his code.

              – Akshay L Aradhya
              Jan 3 at 16:14















              Maybe so, but I still find it useful to answer the question as asked, especially for a new question asker.

              – Paul Becotte
              Jan 3 at 16:17





              Maybe so, but I still find it useful to answer the question as asked, especially for a new question asker.

              – Paul Becotte
              Jan 3 at 16:17











              0














              Here we calculate distance for every point, sort it by distance and get first n points.



              def get_nearest_point(x, y, n_points):
              points_with_dist = # store here pairs (dist, point)
              for point in points:
              points_with_dist.append((
              Distance(point[0], point[1], x, y),
              point
              ))
              result_with_dist = sorted(points_with_dist, key=lambda struct: struct[0])[:n_points]
              result = [p[1] for p in result_with_dist] # we need only points
              return result





              share|improve this answer




























                0














                Here we calculate distance for every point, sort it by distance and get first n points.



                def get_nearest_point(x, y, n_points):
                points_with_dist = # store here pairs (dist, point)
                for point in points:
                points_with_dist.append((
                Distance(point[0], point[1], x, y),
                point
                ))
                result_with_dist = sorted(points_with_dist, key=lambda struct: struct[0])[:n_points]
                result = [p[1] for p in result_with_dist] # we need only points
                return result





                share|improve this answer


























                  0












                  0








                  0







                  Here we calculate distance for every point, sort it by distance and get first n points.



                  def get_nearest_point(x, y, n_points):
                  points_with_dist = # store here pairs (dist, point)
                  for point in points:
                  points_with_dist.append((
                  Distance(point[0], point[1], x, y),
                  point
                  ))
                  result_with_dist = sorted(points_with_dist, key=lambda struct: struct[0])[:n_points]
                  result = [p[1] for p in result_with_dist] # we need only points
                  return result





                  share|improve this answer













                  Here we calculate distance for every point, sort it by distance and get first n points.



                  def get_nearest_point(x, y, n_points):
                  points_with_dist = # store here pairs (dist, point)
                  for point in points:
                  points_with_dist.append((
                  Distance(point[0], point[1], x, y),
                  point
                  ))
                  result_with_dist = sorted(points_with_dist, key=lambda struct: struct[0])[:n_points]
                  result = [p[1] for p in result_with_dist] # we need only points
                  return result






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 17:03









                  Danyla HulchukDanyla Hulchuk

                  1365




                  1365






























                      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%2f54023277%2fi-want-iterate-through-the-for-loop-to-return-three-points%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

                      Mossoró

                      Error while reading .h5 file using the rhdf5 package in R

                      Pushsharp Apns notification error: 'InvalidToken'