get n highest y values from list of coordinate pairs in python 3

  • Last Update :
  • Techknowledgy :

As an alternative, terser, answer you could use nlargest from heapq

nlargest(n, your2DList, key = lambda x: x[-1])

A sorted function takes named argument key which is used to compute a compare key for each value.

>>> sorted([
      [1, 2],
      [3, 4],
      [6, 2],
      [6, 11],
      [1, 5]
   ], key = lambda pair: pair[1])[-n: ]
   [
      [1, 5],
      [6, 11]
   ]

just sort the list using 2nd coord as key, and print the 2 last items:

z = [
   [1, 2],
   [3, 4],
   [6, 2],
   [6, 11],
   [1, 5]
]

print(sorted(z, key = lambda x: x[1])[-2: ])

result:

[
   [1, 5],
   [6, 11]
]

Here's one NumPy based approach with np.argsort -

n = 2 # Number of entries to keep
arr = np.asarray(input_list) # Convert to array
for further processing
out = arr[arr[: , 1].argsort()[-n: ]].tolist()

Another NumPy based one and should be faster with np.argpartition -

out = arr[(-arr[: , 1]).argpartition(n, axis = 0)[: n]].tolist()

Suggestion : 2

Last Updated : 03 Mar, 2022

9

Suggestion : 3

Return a float with the magnitude (absolute value) of x but the sign of y. On platforms that support signed zeros, copysign(1.0, -0.0) returns -1.0.,Return the floor of x, the largest integer less than or equal to x. If x is not a float, delegates to x.__floor__, which should return an Integral value.,If x is equal to the largest positive representable float, return the value of the least significant bit of x, such that the first float smaller than x is x - ulp(x).,Return the ceiling of x, the smallest integer greater than or equal to x. If x is not a float, delegates to x.__ceil__, which should return an Integral value.

>>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
0.9999999999999999
   >>>
   fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
1.0
>>> from math
import exp, expm1
   >>>
   exp(1e-5) - 1 # gives result accurate to 11 places
1.0000050000069649e-05
   >>>
   expm1(1e-5) # result accurate to full precision
1.0000050000166668e-05
sqrt(sum((px - qx) ** 2.0
   for px, qx in zip(p, q)))
def phi(x):
   'Cumulative distribution function for the standard normal distribution'
return (1.0 + erf(x / sqrt(2.0))) / 2.0
>>>
import math
   >>>
   math.nan == math.nan
False
   >>>
   float('nan') == float('nan')
False
   >>>
   math.isnan(math.nan)
True
   >>>
   math.isnan(float('nan'))
True

Suggestion : 4

NumPy, short for Numerical Python, is the fundamental package required for high performance scientific computing and data analysis. It is the foundation on which nearly all of the higher-level tools in this book are built. Here are some of the things it provides:,The scientific Python community is hopeful that there may be a matrix multiplication infix operator implemented someday, providing syntactically nicer alternative to using np.dot. But for now this is the way.,See Table 4-1 for a short list of standard array creation functions. Since NumPy is focused on numerical computing, the data type, if not specified, will in many cases be float64 (floating point).,Feel free to experiment with other distributions for the steps other than equal sized coin flips. You need only use a different random number generation function, like normal to generate normally distributed steps with some mean and standard deviation:

In[13]: data1 = [6, 7.5, 8, 0, 1]

In[14]: arr1 = np.array(data1)

In[15]: arr1
Out[15]: array([6., 7.5, 8., 0., 1.])
In[27]: arr1 = np.array([1, 2, 3], dtype = np.float64)

In[28]: arr2 = np.array([1, 2, 3], dtype = np.int32)

In[29]: arr1.dtype In[30]: arr2.dtype
Out[29]: dtype('float64') Out[30]: dtype('int32')
In[45]: arr = np.array([
   [1., 2., 3.],
   [4., 5., 6.]
])

In[46]: arr
Out[46]:
   array([
      [1., 2., 3.],
      [4., 5., 6.]
   ])

In[47]: arr * arr In[48]: arr - arr
Out[47]: Out[48]:
   array([
      [1., 4., 9.], array([
         [0., 0., 0.],
         [16., 25., 36.]
      ])[0., 0., 0.]
   ])
In[51]: arr = np.arange(10)

In[52]: arr
Out[52]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In[53]: arr[5]
Out[53]: 5

In[54]: arr[5: 8]
Out[54]: array([5, 6, 7])

In[55]: arr[5: 8] = 12

In[56]: arr
Out[56]: array([0, 1, 2, 3, 4, 12, 12, 12, 8, 9])
In[75]: arr[1: 6]
Out[75]: array([1, 2, 3, 4, 64])
In[83]: names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])

In[84]: data = np.random.randn(7, 4)

In[85]: names
Out[85]:
   array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'],
      dtype = '|S4')

In[86]: data
Out[86]:
   array([
      [-0.048, 0.5433, -0.2349, 1.2792],
      [-0.268, 0.5465, 0.0939, -2.0445],
      [-0.047, -2.026, 0.7719, 0.3103],
      [2.1452, 0.8799, -0.0523, 0.0672],
      [-1.0023, -0.1698, 1.1503, 1.7289],
      [0.1913, 0.4544, 0.4519, 0.5535],
      [0.5994, 0.8174, -0.9297, -1.2564]
   ])