dimanche 27 septembre 2020

Problems with np.where and lists with strings

I have 3 lists, where one of them is:

x_axis = ["01-01", "01-02", "01-03"] 

etc. Length of x_axis list is 377.

I also have a list:

y_axis = ["01-01","01-03","01-05"]

etc. Length of y_list is 167.

The third list corresponds to list x_axis with a specific number, where:

hour24 = ["50","0","99"]

etc. Length of hour24 is the same as x_axis, where dates in x_axis correspont with number in hour24.

What I want to do is to extract the numbers from hour24 list, that corresponds with dates in y_axis. That means that not all dates are going to be included. I want a new_hour24 list, which contains specific numbers that has the same dates as dates in y-axis. The problem here is that x_axis list includes all dates, but y_axis list includes only some chosen dates.

What I tried to do:

I tried to extend y_axis list with zeros to contain the same length as x_axis:

for i in range(210):
        y_axis.append("0")

Once I had the same length, I tried using if/else and np.where numpy as:

new_hour24 = []

for i in range(0, len(x_axis)):
        if(np.where((x_axis[i]==y_axis[i]))):
             new_hour24.append(hour24[i])
        else:
             continue

However, this for some reason does not work, and my new list, new_hour24, contains all the original values, just as it jumped over the np.where fact and just appended all of the values no matter what.

Does anyone have any ideas of how I can fix that?

edit

Expected output is a new_hour24 list with length 167. The numbers in list new_hour24 corresponds to dates in y_list.

Aucun commentaire:

Enregistrer un commentaire