l = [[98, 25, 33, 9, 41],
[67, 32, 67, 27, 85],
[38, 79, 52, 40, 58],
[84, 76, 44, 9, 2]]
def fnd(l,value):
for i,v in enumerate(l):
if value in v:
return {'row':i+1,'col':v.index(value)+1}
return {'row':-1,'col':-1}
print(fnd(l,40))
{'row': 3, 'col': 4}
Answer from Kuldeep Singh Sidhu on Stack Overflowl = [[98, 25, 33, 9, 41],
[67, 32, 67, 27, 85],
[38, 79, 52, 40, 58],
[84, 76, 44, 9, 2]]
def fnd(l,value):
for i,v in enumerate(l):
if value in v:
return {'row':i+1,'col':v.index(value)+1}
return {'row':-1,'col':-1}
print(fnd(l,40))
{'row': 3, 'col': 4}
If the number of columns will be constant as shown in the example, you can search using below code.
a = [[98, 25, 33, 9, 41],
[67, 32, 67, 27, 85],
[38, 79, 52, 40, 58],
[84, 76, 44, 9, 2]]
a_ind = [p[x] for p in a for x in range(len(p))] # Construct 1d array as index for a to search efficiently.
def find(x):
return a_ind.index(x) // 5 + 1, a_ind.index(x) % 5 + 1 # Here 5 is the number of columns
print(find(98), find(58), find(40))
#Output
(1, 1) (3, 5) (3, 4)
You can make the distinction explicit by adding another dimension to the array.
>>> a = np.array([1, 2, 3])
>>> a
array([1, 2, 3])
>>> a.transpose()
array([1, 2, 3])
>>> a.dot(a.transpose())
14
Now force it to be a column vector:
>>> a.shape = (3,1)
>>> a
array([[1],
[2],
[3]])
>>> a.transpose()
array([[1, 2, 3]])
>>> a.dot(a.transpose())
array([[1, 2, 3],
[2, 4, 6],
[3, 6, 9]])
Another option is to use np.newaxis when you want to make the distinction:
>>> a = np.array([1, 2, 3])
>>> a
array([1, 2, 3])
>>> a[:, np.newaxis]
array([[1],
[2],
[3]])
>>> a[np.newaxis, :]
array([[1, 2, 3]])
Use double [] when writing your vectors.
Then, if you want a row vector:
row_vector = array([[1, 2, 3]]) # shape (1, 3)
Or if you want a column vector:
col_vector = array([[1, 2, 3]]).T # shape (3, 1)
The format is (m, n) where m is the number of rows and n is the number of columns for a 2d array.
HOWEVER your case is a 1d array, which is why you get (m,). This basically means it's neither rows nor columns, just five items, like a list. There is no second dimension in this case. If you wanted to add a second dimension, there are a bunch of functtions that just happen to slip my mind
In numpy, the shape tuple returns a tuple (m, n), m refers to the "number of rows", whereas n refers to the number of columns.
So (5, 1) would be 5 rows and one column.
And (5,) is just a 1d array, 5 rows... But a 1d vector doesn't isn't referred by the number of columns and rows, since it isn't nested. It's just an array with five elements.
As @TimRoberts mentioned, numpy arrays can have an arbitrary number of dimensions. Such as images, with a shape of a tuple with 3 elements, which is rows, columns and channels, for RGB it would have 3 channels, and for RGBA it would be 4 channels. This would be an example of an 3d array.
Single-element indexing
For single elements indexing as in your example, the result is indeed the same. Although as stated in the docs:
So note that
x[0,2] = x[0][2]though the second case is more inefficient as a new temporary array is created after the first index that is subsequently indexed by 2.
emphasis mine
Array indexing
In this case, not only that double-indexing is less efficient - it simply gives different results. Let's look at an example:
>>> arr = np.array([[1, 2], [3, 4], [5, 6]])
>>> arr[1:][0]
[3 4]
>>> arr[1:, 0]
[3 5]
In the first case, we create a new array after the first index which is all rows from index 1 onwards:
>>> arr[1:]
[[3 4]
[5 6]]
Then we simply take the first element of that new array which is [3 4].
In the second case, we use numpy indexing which doesn't index the elements but indexes the dimensions. So instead of taking the first row, it is actually taking the first column - [3 5].
Using [row][col] is one more function call than using [row, col]. When you are indexing an array (in fact, any object, for that matter), you are calling obj.__getitem__ under the hook. Since Python wraps the comma in a tuple, doing obj[row][col] is the equivalent of calling obj.__getitem__(row).__getitem__(col), whereas obj[row, col] is simply obj.__getitem__((row,col)). Therefore, indexing with [row, col] is more efficient because it has one fewer function call (plus some namespace lookups but they can normally be ignored).
In Python a array does only have one dimension, that's why len(array) returns a single number.
Assuming that you have a 'matrix' in form of array of arrays, like this:
1 2 3
4 5 6
7 8 9
declared like
mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
you can determine the 'number of columns and rows' by the following commands:
rows = len(mat)
columns = len(mat[0])
Note that it only works if number of elements in each row is constant
If you are using numpy to make the arrays, another way to get the column rows and columns is using the tuple from the np.shape() function. Here is a complete example:
import numpy as np
mat = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
rownum = np.shape(mat)[0]
colnum = np.shape(mat)[1]
Such arrays are sometimes called tensors, although you can see there are other definitions that become more relevant in general relativity and abstract algebra. Specifically in the literature on tensor decomposition we find a more specialized lexicon.
For a 3-mode tensor (analogous to 3d array) there are special names:
- Mode 1: columns
- Mode 2: rows
- Mode 3: tubes
The following diagram from Kolda and Bader 2009 is quite clarifying.

But since such tensors are often $n$-modal it is desirable to have the terminology of a mode-$k$ fiber for the $k$th mode of the tensor.
We can access the ith, jth tube in the example array given as follows:
import numpy as np
X = np.array([[[ 0, 1],
[ 2, 3],
[ 4, 5]],
[[ 6, 7],
[ 8, 9],
[10, 11]],
[[12, 13],
[14, 15],
[16, 17]]])
# Let us pick (i,j) = (0,1)
i = 0
j = 1
print(X[i,j,:])
As far as I know, there’s no single name for them. Usually with multi-dimensional data, the dimensions have particular meaning, e.g. the third dimension could be time in time-series, or RGB channels for pictures, use those for naming them.
Rows and columns are popular with tabular data, but for same reason as above, it is usually more informative to talk about samples in rows and features in columns, especially since different software may use different defaults for them, e.g. Python’s Numpy by default assumes samples in columns and features in rows.