Skip to content

Conversation

@ToheartZhang
Copy link

Change No.61 problem to find 3 nearest values in order.
I ran generators.py and it made some other changes in files.


< q61
Find the nearest value from a given value in an array (★★☆)
Find the 3 nearest values in order from a given value in an array (★★☆)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the "in order".

z = 0.5
m = Z.flat[np.abs(Z - z).argmin()]
print(m)
m = Z.flat[np.abs(Z - z).ravel().argpartition(3)[:3]]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefere a one line expression such that people can see directly what is the interesting part. I know that it won't work for all kind of specific cases but the goal here is to highlight argpartition.

print( np.abs(Z - z).argpartition(3)[:3]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants