 # 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)[-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)[-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: data1 = [6, 7.5, 8, 0, 1]

In: arr1 = np.array(data1)

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

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

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

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

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

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

In: arr
Out: 5

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

In: arr[5: 8] = 12

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

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

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

In: data
Out:
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]
])```