Home > Mobile >  Passing an unspecified numpy array as an argument to a function
Passing an unspecified numpy array as an argument to a function

Time:05-13

I want to be able to define a function as having an argument of an unspecified array. For example,

import numpy as np
def cols(np.array([]): 
    return len(np.array([])

Say that:

x=np.array([[1,2,3],[4,5,6]])

Then I want cols(x) to give output 3.

Note, the input must be a np.array, so please no workaround to that!

CodePudding user response:

You don't have to specify what is your argument in python

import numpy as np

def cols(arr):
    return arr.shape[1]

number_of_cols = cols(np.array([[1, 2, 3], [4, 5, 6]]))
print(number_of_cols) # will print 3

  • Related