简体   繁体   中英

How do I get the 10 smallest numbers of an array?

This is my code:

from astropy.io import fits
import pandas
import matplotlib.pyplot as plt
import numpy as np
import heapq 

datos = fits.open('/home/citlali/Descargas/Lista.fits')
data = datos[1].data

#Linea [SIII] 9532
Mask_1 = data['flux_[SIII]9531.1_Re_fit'] / data['e_flux_[SIII]9531.1_Re_fit'] > 5
newdata1 = data[Mask_1]

H1_alpha = newdata1['log_NII_Ha_Re']

H1_beta = newdata1['log_OIII_Hb_Re']

M = H1_alpha < -0.9

newx = H1_alpha[M] #This is my array where I need the smallest 10 numbers
newy = H1_beta[M]  

sm = heapq.nsmallest(10, newx)

plt.plot(sm, newy, 'ro')  

I want the 10 smallest numbers of newx but also I need "y" values (newy) of this numbers and idk how to get them. Thanks.

the documentation for heapq.nsmallest shows that you can give it a key:

heapq.nsmallest(n, iterable, key=None)

this means that you can zip the newx and newy values together and then choose the nsmallest based on the newx values.

M = H1_alpha < -0.9

newx = H1_alpha[M]
newy = H1_beta[M]  

sm = heapq.nsmallest(10, zip(newx, newy), key= lambda x: x[0])

plt.plot([i[0] for i in sm], [i[1] for i in sm], 'ro') 

Combine newx and newy into a list of tuples. Then you can get the 10 smallest from this, and split them back into separate lists to sort.

sm = heapq.nsmallest(10, zip(newx, newy)) # zip them to sort together
newx, newy = zip(*sm) # unzip them
plt.plot(newx, newy, 'ro')

If your data is a basic Python data structure, you can use zip as user @barmar indicated.

import heapq

newx = [10, 20, 30, 40, 50, 60, 70, 80]
newy = [11, 21, 31, 41, 51, 61, 71, 81]

sm = heapq.nsmallest(4, zip(newx, newy))
print(sm)

If you're using pandas , you can use the .nsmallest() method of the series and use the index of the result to get the matching results from the other series (saving you the creation of a 3rd data structure that has a copy of all the data again):

from pandas import Series

newx = Series(newx)
newy = Series(newy)

smx = newx.nsmallest(4)
smy = newy[smx.index]
print(smx, smy)

If you're just using numpy , this works:

import numpy as np

anewx = np.array(newx)
anewy = np.array(newy)

smxi = np.argpartition(anewx, 4)[:4]
print(anewx[smxi])
print(anewy[smxi])

Results of the combined code running:

[(10, 11), (20, 21), (30, 31), (40, 41)]
0    10
1    20
2    30
3    40
dtype: int64 0    11
1    21
2    31
3    41
dtype: int64
[20 10 30 40]
[21 11 31 41]
def get(i, lis):
    lis.sort()
    log = []
    for op in range(i):
        log.append(op)
    return log

mylist = [5, 6, 8, 9, 1, 14, 52, 10, 65, 12, 65]
print(get(10, mylist))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM