I want iterate through the for loop to return three points
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
add a comment |
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
3
Do you have a question?
– jpp
Jan 3 at 13:32
add a comment |
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
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
python
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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]]
add a comment |
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.
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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]]
add a comment |
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]]
add a comment |
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]]
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]]
edited Jan 3 at 16:16
answered Jan 3 at 16:07
Akshay L AradhyaAkshay L Aradhya
1,05811128
1,05811128
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 3 at 17:03
Danyla HulchukDanyla Hulchuk
1365
1365
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
3
Do you have a question?
– jpp
Jan 3 at 13:32