-2

In the following code , i am getting a syntax error near the ':' in the selected_rows and selected_patch I am not able to understand how to rectify it Can someone help me?

def selectNeighboringPatch(matrix, pos_row, pos_col, ex_len):
    selected_rows = matrix([ range(pos_row - ex_len , pos_row + ex_len+1) , : ])
    selected_patch = selected_rows([ : , range(pos_col - ex_len , pos_col + ex_len + 1)])
    return selected_patch
selectNeighboringPatch( matrix = ([[1,1,1,1] ,[1,1,1,1] ,[1,1,1,1] ,[1,1,1,1]]) ,pos_row = 0 ,pos_col = 0 , ex_len = 2 )
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
SU7
  • 1
  • what is `matrix`? this is not a [mcve] - we can not run it and see your error. what is your exact error message and stack trace? they are given for a reason: to see _what_ is wrong. My guess would be that `matrix([ range(pos_row - ex_len , pos_row + ex_len+1) , : ])` has too many () around it - if you want to slice matrix and it is a list ... – Patrick Artner Dec 17 '18 at 16:21
  • If you want to slice,read here: [python slicing syntax](https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation) – Patrick Artner Dec 17 '18 at 16:22

1 Answers1

0
matrix([...])

Is the syntax for calling a callable with a list literal. You would usually see this as something like

sum([1, 2, 3, 4])

The expression in brackets in

selected_rows = matrix([ range(pos_row - ex_len , pos_row + ex_len+1) , : ])

is not a valid list literal, which is the cause of the syntax error. You likely meant

selected_rows = matrix[ range(pos_row - ex_len , pos_row + ex_len+1) , : ]

The : there is a valid python expression (equivalent to slice(None)), but don't expect mulidimensional slices to work on regular python lists the way they would on a pandas dataframe.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96