Related
I know you can iterate over a 2d matrix using two indexes like this:
import numpy as np
A = np.zeros((10,10))
for i in range(0,10):
for j in range(0,10):
if (i==j):
A[i,j] = 4
Is there a way of doing this using only one for loop or using slices?
EDIT:
I also need to take into account of when i =/ j, for example:
A = np.zeros((10,10))
for i in range(0,10):
for j in range(0,10):
if (i==j):
A[i,j] = 1
if (i+1 ==j):
A[i,j] = 2
if (i-1==j):
A[i,j] = 3
You can always collapse multiple loops into one by calculating the components each iteration with the modulo operator like so:
import numpy as np
A = np.zeros((10,10))
for x in range(100):
i = math.floor(x/10)
j = x % 10
if (i==j):
A[i,j] = 1
if (i+1 ==j):
A[i,j] = 2
if (i-1==j):
A[i,j] = 3
With only i==j it could be even simpler:
for i in range(10):
A[i,i] = 4
In [129]: A = np.zeros((10,10), int)
...: for i in range(0,10):
...: for j in range(0,10):
...: if (i==j):
...: A[i,j] = 1
...: if (i+1 ==j):
...: A[i,j] = 2
...: if (i-1==j):
...: A[i,j] = 3
...:
You should have shown the resulting A:
In [130]: A
Out[130]:
array([[1, 2, 0, 0, 0, 0, 0, 0, 0, 0],
[3, 1, 2, 0, 0, 0, 0, 0, 0, 0],
[0, 3, 1, 2, 0, 0, 0, 0, 0, 0],
[0, 0, 3, 1, 2, 0, 0, 0, 0, 0],
[0, 0, 0, 3, 1, 2, 0, 0, 0, 0],
[0, 0, 0, 0, 3, 1, 2, 0, 0, 0],
[0, 0, 0, 0, 0, 3, 1, 2, 0, 0],
[0, 0, 0, 0, 0, 0, 3, 1, 2, 0],
[0, 0, 0, 0, 0, 0, 0, 3, 1, 2],
[0, 0, 0, 0, 0, 0, 0, 0, 3, 1]])
So you have set 3 diagonals:
In [131]: A[np.arange(10),np.arange(10)]
Out[131]: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
In [132]: A[np.arange(9),np.arange(1,10)]
Out[132]: array([2, 2, 2, 2, 2, 2, 2, 2, 2])
In [133]: A[np.arange(1,10),np.arange(9)]
Out[133]: array([3, 3, 3, 3, 3, 3, 3, 3, 3])
The key to eliminating loops in numpy is to get a big picture of the task, rather than focusing on the iterative steps.
There are various tools for making a diagonal array. One is np.diag, which can be used thus:
In [139]: np.diag(np.ones(10,int),0)+
np.diag(np.ones(9,int)*2,1)+
np.diag(np.ones(9,int)*3,-1)
Out[139]:
array([[1, 2, 0, 0, 0, 0, 0, 0, 0, 0],
[3, 1, 2, 0, 0, 0, 0, 0, 0, 0],
[0, 3, 1, 2, 0, 0, 0, 0, 0, 0],
[0, 0, 3, 1, 2, 0, 0, 0, 0, 0],
[0, 0, 0, 3, 1, 2, 0, 0, 0, 0],
[0, 0, 0, 0, 3, 1, 2, 0, 0, 0],
[0, 0, 0, 0, 0, 3, 1, 2, 0, 0],
[0, 0, 0, 0, 0, 0, 3, 1, 2, 0],
[0, 0, 0, 0, 0, 0, 0, 3, 1, 2],
[0, 0, 0, 0, 0, 0, 0, 0, 3, 1]])
Or adapting [131] etc
In [140]: A = np.zeros((10,10), int)
...: A[np.arange(10),np.arange(10)]=1
...: A[np.arange(9),np.arange(1,10)]=2
...: A[np.arange(1,10),np.arange(9)]=3
Because your only executing code when i == j, you can just use:
for i in range(0,10):
A[i,i] = 4
This code generates a pascal triangle:
import pprint
def nextRow(cRow):
cRow.append(0)
return [cRow[m - 1] + cRow[m + 1] for m in range(len(cRow) - 1)]
def Pascal(n):
row = [0, 0, 0, 0, 1, 0, 0, 0, 0]
l = []
for h in range(n):
l.append(row)
row = nextRow(row)
return l
pprint.pprint(Pascal(5))
I am trying to remove the extra zeros without just removing them in the end of the code:
Output:
[[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 2, 0, 1, 0, 0, 0],
[0, 1, 0, 3, 0, 3, 0, 1, 0, 0],
[1, 0, 4, 0, 6, 0, 4, 0, 1, 0]]
Desired Output:
[[0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 2, 0, 1, 0, 0],
[0, 1, 0, 3, 0, 3, 0, 1, 0],
[1, 0, 4, 0, 6, 0, 4, 0, 1]]
You can save in l the row calculated without the last element with l.append(row[:-1]) instead of l.append(row) in the Pascal function.
import pprint
def nextRow(cRow):
cRow.append(0)
return [cRow[m - 1] + cRow[m + 1] for m in range(len(cRow) - 1)]
def Pascal(n):
row = [0, 0, 0, 0, 1, 0, 0, 0, 0]
l = []
for h in range(n):
l.append(row[:-1])
row = nextRow(row)
return l
pprint.pprint(Pascal(5))
Excluding the boundary zero values, is it possible to group the coordinates (as tuples) of remaining zero values into different lists in this numpy array?
[[ 0 0 0 0 0 0 0 0 0 0 0]
[ 0 1 1 1 0 0 0 1 10 2 0]
[ 0 2 10 2 1 0 0 1 2 10 0]
[ 0 10 3 10 1 0 0 0 1 1 0]
[ 0 1 2 1 1 0 0 0 0 0 0]
[ 0 1 2 1 2 2 2 1 0 0 0]
[ 0 10 2 10 2 10 10 1 0 0 0]
[ 0 1 2 1 2 2 2 1 1 1 0]
[ 0 0 0 0 0 0 0 0 1 10 0]
[ 0 0 0 0 0 0 0 0 1 1 0]
[ 0 0 0 0 0 0 0 0 0 0 0]]
for ex. in above grid, there are two 'groups' of zeros, one in lower left corner and other in upper right corner. can these be put into separate lists, for every such matrix generated? Below is the code for creating matrix 'sol_mat' :-
import numpy as np
import random
bomb_mat = np.zeros((11,11), dtype = int)
for i in range(10):
a = random.randint(1,9)
b = random.randint(1,9)
bomb_mat[a,b] = 1
sol_mat = np.zeros(11,11), dtype = int)
for j in range(1,10):
for k in range(1,y-1):
if bomb_mat[j,k] == 1:
sol_mat[j,k] = 10
else:
sol_mat[j,k] = bomb_mat[j-1,k-1] + bomb_mat[j,k-1] + bomb_mat[j+1,k-1]+ bomb_mat[j-1,k] + bomb_mat[j+1,k] + bomb_mat[j-1,k+1] + bomb_mat[j,k+1] + bomb_mat[j+1,k+1]
Trying to create minesweeper
I made some adjustments on your code. Mainly I tried to avoid for loops and I used scipys convolve2d() to create sol_mat. The main advantage of this method is that you don't have to worry about the edge cases of the image. Using a 3x3 kernel of ones on the boolean array of bombs gives you exactly the number of neighbouring bombs (the flags in minesweeper).
import numpy as np
from scipy.signal import convolve2d
grid_size = (7, 7)
n_bombs = 5
bomb_mat = np.zeros(grid_size, dtype=int)
bomb_mat[np.random.randint(low=1, high=grid_size[0]-1, size=n_bombs),
np.random.randint(low=1, high=grid_size[1]-1, size=n_bombs)] = 1
# array([[0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 1, 0, 0],
# [0, 0, 1, 0, 0, 0, 0],
# [0, 1, 1, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0],
# [0, 0, 1, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0]])
sol_mat = convolve2d(bomb_mat, np.ones((3, 3)), mode='same').astype(int)
sol_mat[bomb_mat.astype(bool)] = 10
# array([[ 0, 0, 0, 1, 1, 1, 0],
# [ 0, 1, 1, 2, 10, 1, 0],
# [ 1, 3, 10, 3, 1, 1, 0],
# [ 1, 10, 10, 2, 0, 0, 0],
# [ 1, 3, 3, 2, 0, 0, 0],
# [ 0, 1, 10, 1, 0, 0, 0],
# [ 0, 1, 1, 1, 0, 0, 0]])
You can use np.tril() and np.triu() to get the lower and upper triangle of an array. By building the intersection of boolean triangles with the condition sol_mat == 0 you get the wanted indices:
lower0 = np.logical_and(np.tril(np.ones(grid_size)), sol_mat == 0)
# lower0.astype(int)
# array([[1, 0, 0, 0, 0, 0, 0],
# [1, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 1, 0, 0],
# [1, 0, 0, 0, 1, 1, 0],
# [1, 0, 0, 0, 1, 1, 1]])
upper0 = np.logical_and(np.triu(np.ones(grid_size)), sol_mat == 0)
# upper0.astype(int)
# array([[1, 1, 1, 0, 0, 0, 1],
# [0, 0, 0, 0, 0, 0, 1],
# [0, 0, 0, 0, 0, 0, 1],
# [0, 0, 0, 0, 1, 1, 1],
# [0, 0, 0, 0, 1, 1, 1],
# [0, 0, 0, 0, 0, 1, 1],
# [0, 0, 0, 0, 0, 0, 1]])
You can get the indices of these arrays via np.nonzero():
lower0_idx = np.array(np.nonzero(lower0))
# array([[0, 1, 4, 5, 5, 5, 6, 6, 6, 6],
# [0, 0, 4, 0, 4, 5, 0, 4, 5, 6]])
upper0_idx = np.array(np.nonzero(upper0))
# array([[0, 0, 0, 0, 1, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6],
# [0, 1, 2, 6, 6, 6, 4, 5, 6, 4, 5, 6, 5, 6, 6]])
I need help with shifting and deleting elements in a 2-dimensional array.
If the value in a list is negative and their is a list above it with positive values in the same location. It should shift everything down, causing the negative values to disappear.
If there isn't any list above it or the corresponding values in the list above are just 0. It will replace the negative values with 0.
Scenario 1, 3, and 4 are working! But Scenario 2 doesn't work. (I hope I covered all possible scenarios in my examples)
Note: The positive values should never disappear, they can only move down when needed. Only the negative values (below -100) disappear.
These examples should explain it better:
Scenario 1: # This Works
DATA
[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 1, 3, 1, 0, 0],
[-102, -102, -102, 0, 0],
[ 3, 1, 3, 0, 0]]
EXPECT
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 3, 1, 0, 0],
[3, 1, 3, 0, 0]]
Scenario 2: # Doesn't Work
DATA
[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 1, 2, 1, 0, 0],
[ 2, 1, 2, 0, 0],
[-103, -103, -103, 0, 0]]
EXPECT
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 2, 1, 0, 0],
[2, 1, 2, 0, 0]]
The Current Output (Incorrect):
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 2, 1, 0, 0],
[2, 1, 2, 0, 0],
[0, 0, 0, 0, 0]]
Scenario 3: # This Works
DATA
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, -101, -101, -101],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]]
EXPECT
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 0, 0, 0],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]]
Scenario 4: # This Works
DATA
[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[-101, 2, 2, 3, 4],
[-101, 1, 2, 3, 2],
[-101, 3, 3, 2, 3]]
EXPECT
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 2, 3, 4],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]]
Here is my code - It's definitely overcomplicated for what I am trying to achieve but I have been stuck on this and can't figure out any alternative. (I can't import any outside function...so no numpy)
def move(data):
# PART 1
'''
Creates a list that contains a tuple (row, coll) of the location where a negative value (> -100) appears on the list.
'''
rows = len(data)
c_count = 4
row_list = []
while c_count >= 0:
for x in range(rows):
if data[x][c_count] < -100:
row_list.append((x, c_count))
c_count -=1
# PART 2
'''
Iterates through the list of values that contain negative value (> -100) and performs the actions listed below.
'''
for x in row_list:
row = x[0]
col = x[1]
try: # If there isn't anything above the negative value (except for 0), make all negative value (>-100) == 0. EXAMPLE (DATA 3)
if data[row-1][col] == 0:
data[row][col] = 0
except(IndexError):
pass
try: # If a row of negative values is between a row on top and bottom that contains possitive values, then merge the values above with the negative values below it. EXAMPLE (DATA 1)
if data[row-1][col] > 0 and data[row+1][col] > 0:
c_count = 4
while c_count >= 0:
count = len(data) - 1
prev = count - 1
while count > 0 and prev >= 0:
if data[count][c_count] < -100:
while prev >= 0 and data[prev][c_count] == 0:
prev -= 1
data[count][c_count] = data[prev][c_count]
data[prev][c_count]= 0
count -= 1
prev -= 1
c_count -= 1
return data
except(IndexError):
pass
try: # If a row of negative values has nothing underneath it (at the bottom) of the list. Then push everything on the top down replacing the negative value. This isn't working!
**SCENARIO 2 Should Have Worked Here**
if data[row-1][col] > 0:
data[row][col] = 0
rows = len(data)
c_count = 4
while c_count >= 0:
for x in range(rows):
if data[x][c_count] > 0:
rows = x
break
last = rows-1
if data[last][c_count] == 0:
while last > 0:
data[last][c_count] = data[last-1][c_count]
last -= 1
data[0][c_count] = 0
c_count -= 1
except(IndexError):
pass
return data
print('Data 1') # This Works
data1 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 3, 1, 0, 0],
[-102, -102, -102, 0, 0],
[3, 1, 3, 0, 0]]
print(data1, 'org')
x = move(data1)
expect1 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 3, 1, 0, 0],
[3, 1, 3, 0, 0]]
print(x, 'sol')
print(expect1, 'expect')
print(data1 == expect1)
print()
print('Data 2') # Doesn't Work
data2 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 2, 1, 0, 0],
[2, 1, 2, 0, 0],
[-103, -103, -103, 0, 0]]
print(data2, 'org')
y = move(data2)
expect2 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 2, 1, 0, 0],
[2, 1, 2, 0, 0]]
print(y,'sol')
print(expect2, 'expect')
print(data2 == expect2)
print()
print('Data 3') # This Works
data3 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, -101, -101, -101],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]]
print(data3, 'org')
z = move(data3)
expect3 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 0, 0, 0],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]]
print(z,'sol')
print(expect3, 'expect')
print(data3 == expect3)
print()
print('Data 4') # This Works
data4 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[-101, 2, 2, 3, 4],
[-101, 1, 2, 3, 2],
[-101, 3, 3, 2, 3]]
print(data4, 'org')
a = move(data4)
expect4 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 2, 3, 4],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]]
print(a,'sol')
print(expect4, 'expect')
print(data4 == expect4)
I use code from previous question
Python - Shift/Delete Elements in a 2-Dimensional Array
and it gives correct results:
I work with columns separately, not with full rows.
search in column from bottom to top
find negative value
find positive value (bigger then zero) above
if not found then put zero in place of negative
if found then move down all value above
(move above to row, above-1 to row-1, above-2 to row-2, etc.)
.
def move(data):
# work in column, not with full rows
for col in range(len(data)):
# move from bottom to top
for row in range(len(data[0])-1, -1, -1):
# check if negative value
if data[row][col] < 0:
print('debug: negative:', data[row][col])
# find positive value above
above = row-1
while above > -1 and data[above][col] <= 0:
above -= 1
# check if found positive value
if above == -1:
# put zero if not found value above
print('debug: put zero')
data[row][col] = 0
else:
# move down all values above
print('debug: move down', above+1, 'element(s)')
while above > -1:
data[row][col] = data[above][col]
data[above][col] = 0
row -= 1
above -= 1
return data
# --- function to run one scenario, display data and check result ---
def run(data, expect):
print('data:')
print('\n'.join(str(row) for row in data))
print()
result = move(data)
print()
print('result:')
print(result)
print('expect:')
print(expect)
print('expect == result:', expect == result)
print('---')
# --- scenarios ---
def scenario_B1():
DATA = [
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 1, 3, 1, 0, 0],
[-102, -102, -102, 0, 0],
[ 3, 1, 3, 0, 0]
]
EXPECT = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 3, 1, 0, 0],
[3, 1, 3, 0, 0]
]
run(DATA, EXPECT)
def scenario_B2():
DATA = [
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 1, 2, 1, 0, 0],
[ 2, 1, 2, 0, 0],
[-103, -103, -103, 0, 0]
]
EXPECT = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 2, 1, 0, 0],
[2, 1, 2, 0, 0]
]
run(DATA, EXPECT)
def scenario_B3():
DATA = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, -101, -101, -101],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]
]
EXPECT = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 0, 0, 0],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]
]
run(DATA, EXPECT)
def scenario_B4():
DATA = [
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[-101, 2, 2, 3, 4],
[-101, 1, 2, 3, 2],
[-101, 3, 3, 2, 3]
]
EXPECT = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 2, 3, 4],
[0, 1, 2, 3, 2],
[0, 3, 3, 2, 3]
]
run(DATA, EXPECT)
# --- start scenarios ---
scenario_B1()
scenario_B2()
scenario_B3()
scenario_B4()
Results:
data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 3, 1, 0, 0]
[-102, -102, -102, 0, 0]
[3, 1, 3, 0, 0]
debug: negative: -102
debug: move down 3 element(s)
debug: negative: -102
debug: move down 3 element(s)
debug: negative: -102
debug: move down 3 element(s)
result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 3, 1, 0, 0], [3, 1, 3, 0, 0]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 3, 1, 0, 0], [3, 1, 3, 0, 0]]
expect == result: True
---
data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 2, 1, 0, 0]
[2, 1, 2, 0, 0]
[-103, -103, -103, 0, 0]
debug: negative: -103
debug: move down 4 element(s)
debug: negative: -103
debug: move down 4 element(s)
debug: negative: -103
debug: move down 4 element(s)
result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 2, 1, 0, 0], [2, 1, 2, 0, 0]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 2, 1, 0, 0], [2, 1, 2, 0, 0]]
expect == result: True
---
data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 2, -101, -101, -101]
[0, 1, 2, 3, 2]
[0, 3, 3, 2, 3]
debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero
result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect == result: True
---
data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[-101, 2, 2, 3, 4]
[-101, 1, 2, 3, 2]
[-101, 3, 3, 2, 3]
debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero
result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 2, 3, 4], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 2, 3, 4], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect == result: True
---
I'm trying to find an elegant way to find the max value in a two-dimensional array.
for example for this array:
[0, 0, 1, 0, 0, 1] [0, 1, 0, 2, 0, 0][0, 0, 2, 0, 0, 1][0, 1, 0, 3, 0, 0][0, 0, 0, 0, 4, 0]
I would like to extract the value '4'.
I thought of doing a max within max but I'm struggling in executing it.
Another way to solve this problem is by using function numpy.amax()
>>> import numpy as np
>>> arr = [0, 0, 1, 0, 0, 1] , [0, 1, 0, 2, 0, 0] , [0, 0, 2, 0, 0, 1] , [0, 1, 0, 3, 0, 0] , [0, 0, 0, 0, 4, 0]
>>> np.amax(arr)
Max of max numbers (map(max, numbers) yields 1, 2, 2, 3, 4):
>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]
>>> map(max, numbers)
<map object at 0x0000018E8FA237F0>
>>> list(map(max, numbers)) # max numbers from each sublist
[1, 2, 2, 3, 4]
>>> max(map(max, numbers)) # max of those max-numbers
4
Not quite as short as falsetru's answer but this is probably what you had in mind:
>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]
>>> max(max(x) for x in numbers)
4
How about this?
import numpy as np
numbers = np.array([[0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]])
print(numbers.max())
4
>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]
You may add key parameter to max as below to find Max value in a 2-D Array/List
>>> max(max(numbers, key=max))
4
One very easy solution to get both the index of your maximum and your maximum could be :
numbers = np.array([[0,0,1,0,0,1],[0,1,0,2,0,0],[0,0,2,0,0,1],[0,1,0,3,0,0],[0,0,0,0,4,0]])
ind = np.argwhere(numbers == numbers.max()) # In this case you can also get the index of your max
numbers[ind[0,0],ind[0,1]]
This approach is not as intuitive as others but here goes,
numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]
maximum = -9999
for i in numbers:
maximum = max(maximum,max(i))
return maximum"