0

I want to do a numpy.where() statement where if the length of the PC_list is equal to the length of the numbers then I want the standard deviation to be calculated of the PC_list[0:number].std() and also I want to delete the first number in PC_list: np.setdiff1d(PC_list,PC_list[0]). I want this process to go on until the list number is not equal to number len(PC_list[0:number]!=number. I tried to format numpy.where() that way however I get a syntax error when done that, so I need a correction on the numpy.where() function below.

Code:

number = 3
list_= np.array([457.334015,424.440002,394.795990,408.903992,398.821014,402.152008,435.790985,423.204987,411.574005,
404.424988,399.519989,377.181000,375.467010,386.944000,383.614990,375.071991,359.511993,328.865997,
320.510010,330.079010,336.187012,352.940002,365.026001,361.562012,362.299011,378.549011,390.414001,
400.869995,394.773010,382.556000])
np.where(len(PC_list[0:number]==number,default = 'NA', PC_list[0:number].std(), np.setdiff1d(PC_list,PC_list[0])))

Vanilla Python Code:

from copy import deepcopy
list_ = deepcopy(PC_list)
if len(list_[0:number]==number:
   STD = list_[0:number].std()
   list_ = np.setdiff1d(list_,list_[0])

Error:

  File "<ipython-input-93-215f70228bdc>", line 1
    np.where(len(PC_list[0:number]==number,default = 'NA', PC_list[0:number].std(), np.setdiff1d(PC_list,PC_list[0])))
                                                          ^
SyntaxError: positional argument follows keyword argument
tony selcuk
  • 709
  • 3
  • 11
  • 2
    `where` takes 3 arguments, `cond` and `a` and `b`. In complex cases you should create those one separate lines. That's way they'll be easier to read and debug. – hpaulj Jan 21 '21 at 05:24
  • 2
    alalways put full error message (starting at word "Tracebak") in question (not commet) as text (not screeshot). There are other useful information. – furas Jan 21 '21 at 05:29
  • 1
    it seems like you have all in one `len()` and you have something missing between `default = 'NA'` and `PC_list[0:number].std()`. Maybe put code in functions and make it simpler `where(filter(), func1, func2)` – furas Jan 21 '21 at 05:32
  • @hpaulj I added the error message also updated the code as the vanilla python version. – tony selcuk Jan 21 '21 at 05:49
  • did you try closing the parenthesis for `len(PC_list[0:number]==number,` – anurag Jan 21 '21 at 05:59
  • @anurag I have done that, yea sorry was a foolish mistake – tony selcuk Jan 21 '21 at 23:01
  • In that case try placing this `default = 'NA'` argument at the end of the function, bcz defaults must come **after** positional arguments! – anurag Jan 22 '21 at 05:46
  • @anurag I get an invalid syntax error, can I have 2 options for the formatting, I want to calculate the standard deviation `PC_list[0:number].std()` and then delete the first index in the list `np.setdiff1d(PC_list,PC_list[0])`: the entire function : `np.where(len(PC_list[0:number])!=number,PC_list[0:number].std()np.setdiff1d(PC_list,PC_list[0]), default = 'NA')` – tony selcuk Jan 22 '21 at 06:01

0 Answers0