Azam
Azam

Reputation: 147

Python create new arrays after matching some values from two dimensional arrays

How to search array at some selected values?. For this case I am searching the respective "direction" and "time" for the rain values at "5".

direction=['200','250','180','200','300','270','005','080']
time=['0000','0030','0100','0130','0200','0230','0300','0300']
rain=['15','20','100','5','23','12','5','30']

The expected arrays are like this:

new array=[['200','0130','5'],['005','0300','5']]

Anyone got any idea?. What shall I do after reshaping the data like this:

data3=np.zeros((len(direction1),3),dtype='object')
data3[:,0]=direction1
data3[:,1]=time1
data3[:,2]=rain

Upvotes: 1

Views: 29

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Simply with zip() function:

direction = ['200','250','180','200','300','270','005','080']
time = ['0000','0030','0100','0130','0200','0230','0300','0300']
rain = ['15','20','100','5','23','12','5','30']

result = [list(t) for t in zip(direction, time, rain) if t[-1] == '5']
print(result)

The output:

[['200', '0130', '5'], ['005', '0300', '5']]

Upvotes: 2

Related Questions