You can use np.where to return a tuple of arrays of x and y indices where a given condition holds in an array.
If a is the name of your array:
>>> np.where(a == 1)
(array([0, 0, 1, 1]), array([0, 1, 2, 3]))
If you want a list of (x, y) pairs, you could zip the two arrays:
>>> list(zip(*np.where(a == 1)))
[(0, 0), (0, 1), (1, 2), (1, 3)]
Or, even better, @jme points out that np.asarray(x).T can be a more efficient way to generate the pairs.
You can use np.where to return a tuple of arrays of x and y indices where a given condition holds in an array.
If a is the name of your array:
>>> np.where(a == 1)
(array([0, 0, 1, 1]), array([0, 1, 2, 3]))
If you want a list of (x, y) pairs, you could zip the two arrays:
>>> list(zip(*np.where(a == 1)))
[(0, 0), (0, 1), (1, 2), (1, 3)]
Or, even better, @jme points out that np.asarray(x).T can be a more efficient way to generate the pairs.
Using numpy, argwhere may be the best solution:
import numpy as np
array = np.array([[1, 1, 0, 0],
[0, 0, 1, 1],
[0, 0, 0, 0]])
solutions = np.argwhere(array == 1)
print(solutions)
>>>
[[0 0]
[0 1]
[1 2]
[1 3]]
You should return 'the element has not been found' only after checking all the values, so you must remove the else, and push the second return outside of the for loops
Do not return in else. Program will return immediately after index 0, 0. Return fail value after both loops are done.
You need to iterate over your main list and then you can use list.index() to find the sub-list index, for example:
def index_2d(data, search):
for i, e in enumerate(data):
try:
return i, e.index(search)
except ValueError:
pass
raise ValueError("{!r} is not in list".format(search))
And it will act exactly as list.index() but for a 2D array, so in your case:
position = index_2d(board, "18") # (4, 3)
print(board[position[0]][position[1]]) # 18
position = index_2d(board, "181") # ValueError: '181' is not in list
ind = np.where(np.array(board) == str(place1)) will return the indices of all elements in the board array equal to place. To replace those values do this: board[ind] = newval.
Basically,
import numpy as np
ind = np.where(np.array(board) == str(place1))
board[ind] = newval
If you have
a=[[1,1],[2,1],[3,1]]
b=[[1,2],[2,2],[3,2]]
Then
a[1][1]
Will work fine. It points to the second column, second row just like you wanted.
I'm not sure what you did wrong.
To multiply the cells in the third column you can just do
c = [a[2][i] * b[2][i] for i in range(len(a[2]))]
Which will work for any number of rows.
Edit: The first number is the column, the second number is the row, with your current layout. They are both numbered from zero. If you want to switch the order you can do
a = zip(*a)
or you can create it that way:
a=[[1, 2, 3], [1, 1, 1]]
If you want do many calculation with 2d array, you should use NumPy array instead of nest list.
for your question, you can use:zip(*a) to transpose it:
In [55]: a=[[1,1],[2,1],[3,1]]
In [56]: zip(*a)
Out[56]: [(1, 2, 3), (1, 1, 1)]
In [57]: zip(*a)[0]
Out[57]: (1, 2, 3)
First you need to access to your columns , so you can do that job with zip(*sudokuBoard) then for insert a value , you must check for existence the value in a proper row and column ! Note that you have your rows in sudokuColumn !
columns=map(list,zip(*sudokuBoard))
sudokuBoard=[[0 for sudokuRow in range(0,int(boardSize))] for sudokuColumn in range(0,int(boardSize))]
def insert_value(your_list,value,row,col):
if value not in columns[col] and value not in your_list[row]:
your_list[row][col]=value
else:
raise ValueError("you can not insert a duplicate value !!")
Try this:
def inBoard(value):
for row in sudokuBoard:
if value in row:
return True
return False
With this you can do something like this:
if inBoard(3):
print "already in board"
else:
print "well played"
You don't need to define no_classes yourself. Use enumerate():
def in_list(c, classes):
for i, sublist in enumerate(classes):
if c in sublist:
return i
return -1
Use list.index(item)
a = [[1,2],[3,4,5]]
def in_list(item,L):
for i in L:
if item in i:
return L.index(i)
return -1
print in_list(3,a)
# prints 1
This is an implementation without using numpy. It is not that efficient, but works fine.
rows = eval(input("How many rows in the list:"))
m = []
for row in range(rows):
value = eval(input("Enter a row:"))
m.append(value)
large = m[0][0]
x = 0
y = 0
for i in range(0, rows):
for j in range(0, len(m[i])):
if(large < m[i][j]):
large = m[i][j]
y = i
x = j
print(x, y, large)
This gives the (row,column) index of the max -
import numpy as np
m = np.array([[1,5,6],[2,6,7],[5,26,12]]) # input minified
print(m)
print np.unravel_index(m.argmax(), m.shape) # main code you need
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)