0

I would like to know how to delete elements of my array that contain the value 0.

My code is as follows:

EL1_array = list(zip(time_list, EL1_list))
print(EL1_array)
for (i,j) in EL1_array:
    if j == 0:
        EL1_array.remove(j)
        print(EL1_array)

This is a sample of the output (the whole thing is too big to post here I think):

[(0.0, 177.051636), (0.01, 0.0), (0.02, 0.0), (0.03, 0.0), (0.04, 0.0), (0.05, 0.0), (0.06, 233.906097), (0.07, 242.052551), (0.08, 249.754761), (0.09, 257.026062), (0.1, 263.887482), (0.11, 270.364075), (0.12, 276.482758), (0.13, 282.270172), (0.14, 287.751526), (0.15, 292.94986), (0.16, 297.885254), (0.17, 302.574249), (0.18, 307.029663), (0.19, 311.259705), (0.2, 315.267456), (0.21, 319.049622), (0.22, 322.595276), (0.23, 325.883636), (0.24, 328.881287), (0.25, 331.538147), (0.26, 333.782562), (0.27, 335.515656), (0.28, 336.606415), (0.29, 336.892151), 

I am trying to remove all the elements (i, j) that contain j = 0.

The two lists I have are time_list and EL1_list which both contain 300 elements. EL1 is the list that contains the zeroes I want to remove. The problem is I want to plot the EL1_lit against the time_list without the zeroes included hence my desire to remove them from the EL1_list. So my thinking was to create an array of the two lists and then remove the zeroes with the corresponding time element from the time_list so as they each contain the same number of elements.

  • You say that you want to remove elements with j = 0, then why did you include `i` in `if i or j == 0`? – mkrieger1 Jan 21 '23 at 14:50
  • I was trying to remove the element (i, j) if j =0 – Rory Fitzpatrick Jan 21 '23 at 14:54
  • `if i or j == 0` This does not do what you think. See https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value – John Gordon Jan 21 '23 at 15:16
  • Thanks but I am still unsure how i and j work here? – Rory Fitzpatrick Jan 21 '23 at 15:52
  • do you need to completely drop (0.01, 0.0) since the j element is 0? – Shirin Yavari Jan 21 '23 at 16:52
  • I think so as I would like to plot them without the data that includes j=0 elements. Sorry if that is not enough info I know its not much to go on – Rory Fitzpatrick Jan 21 '23 at 16:59
  • I have managed to isolate the pairs i want to delete using the following code: EL1_array = list(zip(time_list, EL1_list)) print(EL1_array) for (x,y) in EL1_array : if y==0: print(x,y) – Rory Fitzpatrick Jan 21 '23 at 17:02
  • This gives 0.01 0.0 0.02 0.0 0.03 0.0 0.04 0.0 0.05 0.0 0.32 0.0 0.33 0.0 0.34 0.0 0.35 0.0 0.49 0.0 0.5 0.0 0.51 0.0 0.52 0.0 0.53 0.0 0.54 0.0 0.55 0.0 0.56 0.0 0.57 0.0 0.58 0.0 0.59 0.0 etc – Rory Fitzpatrick Jan 21 '23 at 17:03
  • But I dont know how to delete this from the array itself – Rory Fitzpatrick Jan 21 '23 at 17:04
  • EL1_no_zeros= [e for e in EL1_array if e[1]!=0] – Shirin Yavari Jan 21 '23 at 17:09
  • Thanks that worked ! Could you explain to me each step please if you have time. – Rory Fitzpatrick Jan 21 '23 at 17:21
  • Your EL1_array is a list of paired values that you called i and j. e for e in EL1_array iterates through the elements of the list and the if e[1] != 0 makes sure that the jth part of each e (or each element) is not equal to 0. So technically you are creating a new list with iterating through the elements of your first list and only picking the ones that their second part (index 1) is not equal to 0. – Shirin Yavari Jan 21 '23 at 17:27

0 Answers0