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 OverflowVideos
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}
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)
It is said we can loop through 2d matrix by rows or by columns (in algo or python) … I think that the first indice in array is row.[…] … … I give example of stackoverflow link https://stackoverflow.com/questions/10148818/numpy-how-to-iterate-over-columns-of-array, but they said it is because it is Numpy. …
https://imgur.com/Vzk3Cef this is how I learn.
Here in java
int[][] twoDArray = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; System.out.println(twoDArray[1][2]);
Output:
6
If i consider searching by column, the output will be 8. That is what i mean by loop through column first. …
Am i really wrong???
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.
>>> import numpy as np
>>> A = np.array([[1,2,3,4],[5,6,7,8]])
>>> A
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
>>> A[:,2] # returns the third columm
array([3, 7])
See also: "numpy.arange" and "reshape" to allocate memory
Example: (Allocating a array with shaping of matrix (3x4))
nrows = 3
ncols = 4
my_array = numpy.arange(nrows*ncols, dtype='double')
my_array = my_array.reshape(nrows, ncols)
Could it be that you're using a NumPy array? Python has the array module, but that does not support multi-dimensional arrays. Normal Python lists are single-dimensional too.
However, if you have a simple two-dimensional list like this:
A = [[1,2,3,4],
[5,6,7,8]]
then you can extract a column like this:
def column(matrix, i):
return [row[i] for row in matrix]
Extracting the second column (index 1):
>>> column(A, 1)
[2, 6]
Or alternatively, simply:
>>> [row[1] for row in A]
[2, 6]