Max Value in List Array - python-3.x

The following code creates a list with entered values:
def locateLargest():
matrix = []
numberOfRows = int(input("Enter the number of rows: "))
numberOfColumns = 2
for row in range(0, numberOfRows):
matrix.append([])
for column in range(0, numberOfColumns):
value = int(input("Enter a value: "))
matrix[row].append(value)
max_value = None
for value in matrix:
if not max_value:
max_value = value
elif value > max_value:
max_value = value
print(max_value)
locateLargest()
The issue I am running into is that it is asking for each value individual in the row, and is returning the maximum pair of values in the row, not the maximum value's index.
The sample run of what I should be getting is:
Enter the number of rows in the list: 3
Enter a row: 23.5 35 2 10
Enter a row: 4.5 3 45 3.5
Enter a row: 35 44 5.5 11.6
The location of the largest element is at (1,2)
Any ideas?
My current output is:
Enter the number of rows: 2
Enter the number of columns: 6
Enter a value: 2
Enter a value: 2
Enter a value: 2
Enter a value: 2
Enter a value: 2
Enter a value: 2
Enter a value: 7
Enter a value: 6
Enter a value: 4
Enter a value: 3
Enter a value: 6
Enter a value: 2
[7, 6, 4, 3, 6, 2]

This is not very 'pythonic' but will help you achieve your end goal and hopefully understand the process. As Ɓukasz mentioned, you need to do an iteration for each row, and for each column in each row:
First declare the variable to store your location:
maxPoint = [0,0]
Then enumerate your matrix such that you can get the list from each row, but also retrieve the index of the currently active row:
for idx, row in enumerate(matrix):
Find the max value in the current list of values, ie: [10, 20, 30]
maxRowValue = max(row)
Find which column this maximum value lives in, ie: [0, 1, 2, ...]
maxRowIndex = row.index(maxRowValue)
Determine if max row value is in fact greater than any other previously located points, if it is less discard it:
if maxRowValue <= matrix[maxPoint[0]][maxPoint[1]]:
continue
If the value is greater, save it to the maxPoint variable:
maxPoint = [idx, maxRowIndex]
EDIT
For the sake of completeness, here is the complete code sample with AChampion's performance improvements added:
def locateLargest():
matrix = []
numberOfRows = int(input("Enter the number of rows: "))
numberOfColumns = 2
for row in range(0, numberOfRows):
matrix.append([])
for column in range(0, numberOfColumns):
value = int(input("Enter a value: "))
matrix[row].append(value)
maxPoint = [0,0]
for rIndex, row in enumerate(matrix):
cIndex, maxRowValue = max(enumerate(row), key=lambda x: x[1])
if maxRowValue <= matrix[maxPoint[0]][maxPoint[1]]:
continue
maxPoint = [rIndex, cIndex]
print(maxPoint)
locateLargest()
EDIT 2
Here is the same algorithm without using enumerate:
currentRow = 0
for row in matrix:
maxRowValue = max(row)
maxRowIndex = row.index(maxRowValue)
if maxRowValue > matrix[maxPoint[0]][maxPoint[1]]:
maxPoint = [currentRow, maxRowIndex]
currentRow += 1

Using enumerate() and some generator expressions, you can reduce this code quite a bit:
Generate the rows
Generate the maximum for each row
Find the maximum across all rows
More complex perhaps than some would like:
numberOfRows = int(input("Enter the number of rows: "))
# Generate the rows
rows = (map(int, input("Enter a row: ").split()) for _ in range(numberOfRows))
# Generate the maximum for each row
max_row = (max(enumerate(data), key=lambda x: x[1]) for data in rows)
# Find the maximum across all rows
i, (j, v) = max(enumerate(max_row), key=lambda x: x[1][1])
print("The location of the largest element is at {} [{}]".format((i, j), v))
Input / Output:
Enter the number of rows: 3
Enter a row: 1 2 3
Enter a row: 3 6 3
Enter a row: 1 2 3
'The location of the largest element is at (1, 1) [6]'
If you want to see what is going on change the generators to list comprehensions:
>>> rows = [list(map(int, input("Enter a row: ").split())) for _ in range(numberOfRows)]
Enter a row: 1 2 3
Enter a row: 3 6 3
Enter a row: 1 2 3
>>> rows
[[1, 2, 3], [3, 6, 3], [1, 2, 3]]
>>> max_row = [max(enumerate(data), key=lambda x: x[1]) for data in rows]
>>> max_row
[(2, 3), (1, 6), (2, 3)]
>>> list(enumerate(max_row))
[(0, (2, 3), (1, (1, 6)), (2, (2, 3))]
^^^^^^^^^
i, (j, v)

Related

Counting the number of one in a list

I already know the number of columns which is 3. The program asks the user to enter the number of rows
After that, the program tries to see for each row if the number of one entered by the user for each line is greater or equal to 2 and do the sum of the number of rows that the one is greater or equal to one
like if we have entered 3 like a number of rows. we have to enter the value that is either 0 or one for each row (each row has three-element) let's join these values
1 0 1
1 1 1
0 1 0
the program will print 2(Because the row 1 and row 2 have the number of one greater than one each)
Here is the code that I wrote. But I'm unable to count the number of one for each row
tab=[]
ligne=int(input('Enter rows : '))
column=1
for i in range(ligne):
a=[]
for k in range(column):
new=(input())
so=new.split()
a.append(new)
print()
tab.append(a)
for i in range(ligne):
for k in range(column):
for c in range(len(so)):
so[i][k] = int(so[i][k])
count = 0
for row in matrix:
if row.count(1) > 1:
count += 1
Input:
# I hardcoded the matrix for demo.
matrix = [
[1, 0, 1],
[1, 1, 1],
[0, 1, 0],
]
Output:
>>> print(count)
>>> 2

python matrix multiplication check if number of rows of 1st matrix is equal to number of columns of 2nd matrix

I need to perform a matrix multiplication between 2 matrices by taking user input. The below code works fine for the multiplication part but if the no. of rows of 1st matrix are not equal to the no. of columns of the 2nd matrix then it should print NOT POSSIBLE and exit. But it still goes on to add the elements of the matrices. What could possibly be wrong in this code & what could be the solution for the same. Any help would be greatly appreciated
def p_mat(M,row_n,col_n):
for i in range(row_n):
for j in range(col_n):
print(M[i][j],end=" ")
print()
def mat_mul(A_rows,A_cols,A,B_rows,B_cols,B):
if A_cols != B_rows:
print("NOT POSSIBLE")
else:
C = [[0 for i in range(B_cols)] for j in range(A_rows)]
for i in range(A_rows) :
for j in range(B_cols) :
C[i][j] = 0
for k in range(B_rows) :
C[i][j] += A[i][k] * B[k][j]
p_mat(C, A_rows, B_cols)
if __name__== "__main__":
A_rows = int(input("Enter number of rows of 1st matrix: "))
A_cols = int(input("Enter number of columns of 1st matrix: "))
B_rows = int(input("Enter number of rows of 2nd matrix: "))
B_cols = int(input("Enter number of columns of 2nd matrix: "))
##### Initialization of matrix A and B #####
A = [[0 for i in range(B_cols)] for j in range(A_rows)]
B = [[0 for i in range(B_cols)] for j in range(A_rows)]
print("Enter the elements of the 1st matrix: ")
for i in range(A_rows):
for j in range(A_cols):
A[i][j] = int(input("A[" + str(i) + "][" + str(j) + "]: "))
print("Enter the elements of the 2nd matrix: ")
for i in range(B_rows):
for j in range(B_cols):
B[i][j] = int(input("B[" + str(i) + "][" + str(j) + "]:"))
##### Print the 1st & 2nd matrices #####
print("First Matrix : ")
p_mat(A,A_rows,A_cols)
print("Second Matrix : ")
p_mat(B,B_rows,B_cols)
### Function call to multiply the matrices ###
mat_mul(A_rows,A_cols,A,B_rows,B_cols,B)
For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix.
If you want to check the no of rows of 1st matrix and the no. of columns of the 2nd matrix then change the if A_cols != B_rows to if A_rows != B_cols
With your current code, it will print NOT POSSIBLE when A_cols != B_rows which is right.
Ex.
Enter number of rows of 1st matrix: 2
Enter number of columns of 1st matrix: 3
Enter number of rows of 2nd matrix: 2
Enter number of columns of 2nd matrix: 3
Enter the elements of the 1st matrix:
A[0][0]: 1
A[0][1]: 2
A[0][2]: 3
A[1][0]: 4
A[1][1]: 5
A[1][2]: 6
Enter the elements of the 2nd matrix:
B[0][0]:1
B[0][1]:2
B[0][2]:3
B[1][0]:4
B[1][1]:5
B[1][2]:6
First Matrix :
1 2 3
4 5 6
Second Matrix :
1 2 3
4 5 6
NOT POSSIBLE
Another mistake in the code is when you are initialize the Matrices.You are doing
A = [[0 for i in range(B_cols)] for j in range(A_rows)]
B = [[0 for i in range(B_cols)] for j in range(A_rows)]
If the B_cols are smaller than the A_cols when you adding elements in A it will raise IndexError
The same if the B_cols are greater than A_cols when you are adding elements to B will raise IndexError.
Change it to
A = [[0 for i in range(A_cols)] for j in range(A_rows)]
B = [[0 for i in range(B_cols)] for j in range(B_rows)]

A variable will be changed for every iteration and I want to print it into a dictionary as key

for i in range(N):
k1,k2,k3,k4 = input().split(' ')
k2 = float(k2)
k3 = float(k3)
k4 = float(k4)
score = (k2+k3+k4)/3
a = {print(k1) : score}
The input has 4 values: string, int, int, int.
I want to set the value of k1 as key and the score as value.
When I tried the above code, this was the output:
1
You first need to initialise the dictionary and then you can insert items into it with dictionary[key] = value
# Initialise a dictionary where we can store values to
a = {}
# How many times we will ask for values
n = int(input('number of values: '))
for i in range(n):
k1,k2,k3,k4 = input('give input: ').split(' ')
k2 = float(k2)
k3 = float(k3)
k4 = float(k4)
score = (k2+k3+k4)/3
# Store the score value to k1 key
a[k1] = score
# Print the dictionary after the loop has been completed
print(a)
This will do the following:
number of values: 3
give input: first 1 2 3
give input: second 4 5 6
give input: third 10 20 30
{'first': 2.0, 'second': 5.0, 'third': 20.0}
Here's the code that work (very small modification from your original code)
for i in range(4):
k1, k2, k3, k4 = input().split(" ")
k2 = float(k2)
k3 = float(k3)
k4 = float(k4)
score = (k2 + k3 + k4) / 3
print({k1: score})
If you run it with the value of 1 10 20 30 the script returns:
{'1': 20.0}

Inserting Elements into a 2 Dimensional List

elements = []
i,j = 0,0
while(i<3):
while(j<3):
elements[i][j] = int(input())
j+=1
i+=1
j=0
print(elements)
I'm trying to insert elements into 2 dimensional list by getting the input from the user. I'm unable to do so, its giving me a IndexError.
IndexError: list assignment index out of range
I'm expecting a 3x3 list.
Something like :
elements = [
[0,1,2],
[3,4,5],
[6,7,8]
]
What am I doing wrong here? [I do not wish to use Numpy or other libraries atm]
Problem with your case is that the list is initialized with size 0 and as a empty list. So, when you have to set value at some index it throws up error saying that the specified index is out of range because the index doesn't exist.
My approach mutates the existing list in-place or in other words appends a value.
Get size as input first
>>> rows = int(input("Enter no. of rows: "))
Enter no. of rows: 2
>>> cols = int(input("Enter no. of Columns: "))
Enter no. of Columns: 2
Create a list and loop through ranges
>>> l = []
>>> for i in range(rows):
... row_vals = []
... for j in range(cols):
... row_vals.append(int(input(f"Enter value at {i}th row and {j}th column: ")))
... l.append(row_vals)
...
Enter value at 0th row and 0th column: 0
Enter value at 0th row and 1th column: 1
Enter value at 1th row and 0th column: 1
Enter value at 1th row and 1th column: 0
>>> l
[[0, 1], [1, 0]]
This will sove your problem:
elements = []
i, j = 0,0
while(i<3):
elements.append([])
while(j<3):
elements[i].append(int(input()))
j+=1
i+=1
j = 0
print(elements)
The points:
Lists in python are not automatically appended when you access an index, you have to build the list.
You forgot to zero the "j" counter, so that it starts correctly in each row.
Cheers!

how to get a kind of "maximum" in a matrix, efficiently

I have the following problem: I have a matrix opened with pandas module, where each cell has a number between -1 and 1. What I wanted to find is the maximum "posible" value in a row that is also not the maximum value in another row.
If for example 2 rows has their maximum value at the same column, I compare both values and take the bigger one, then for the row that has its maximum value smaller that the other row, I took the second maximum value (and do the same analysis again and again).
To explain myself better consider my code
import pandas as pd
matrix = pd.read_csv("matrix.csv")
# this matrix has an id (or name) for each column
# ... and the firt column has the id of each row
results = pd.DataFrame(np.empty((len(matrix),3),dtype=pd.Timestamp),columns=['id1','id2','max_pos'])
l = len(matrix.col[[0]]) # number of columns
while next = 1:
next = 0
for i in range(0, len(matrix)):
max_column = str(0)
for j in range(1, l): # 1 because the first column is an id
if matrix[max_column][i] < matrix[str(j)][i]:
max_column = str(j)
results['id1'][i] = str(i) # I coul put here also matrix['0'][i]
results['id2'][i] = max_column
results['max_pos'][i] = matrix[max_column][i]
for i in range(0, len(results)): #now I will check if two or more rows have the same max column
for ii in range(0, len(results)):
# if two id1 has their max in the same column, I keep it with the biggest
# ... max value and chage the other to "-1" to iterate again
if (results['id2'][i] == results['id2'][ii]) and (results['max_pos'][i] < results['max_pos'][ii]):
matrix[results['id2'][i]][i] = -1
next = 1
Putting an example:
#consider
pd.DataFrame({'a':[1, 2, 5, 0], 'b':[4, 5, 1, 0], 'c':[3, 3, 4, 2], 'd':[1, 0, 0, 1]})
a b c d
0 1 4 3 1
1 2 5 3 0
2 5 1 4 0
3 0 0 2 1
#at the first iterarion I will have the following result
0 b 4 # this means that the row 0 has its maximum at column 'b' and its value is 4
1 b 5
2 a 5
3 c 2
#the problem is that column b is the maximum of row 0 and 1, but I know that the maximum of row 1 is bigger than row 0, so I take the second maximum of row 0, then:
0 c 3
1 b 5
2 a 5
3 c 2
#now I solved the problem for row 0 and 1, but I have that the column c is the maximum of row 0 and 3, so I compare them and take the second maximum in row 3
0 c 3
1 b 5
2 a 5
3 d 1
#now I'm done. In the case that two rows have the same column as maximum and also the same number, nothing happens and I keep with that values.
#what if the matrix would be
pd.DataFrame({'a':[1, 2, 5, 0], 'b':[5, 5, 1, 0], 'c':[3, 3, 4, 2], 'd':[1, 0, 0, 1]})
a b c d
0 1 5 3 1
1 2 5 3 0
2 5 1 4 0
3 0 0 2 1
#then, at the first itetarion the result will be:
0 b 5
1 b 5
2 a 5
3 c 2
#then, given that the max value of row 0 and 1 is at the same column, I should compare the maximum values
# ... but in this case the values are the same (both are 5), this would be the end of iterating
# ... because I can't choose between row 0 and 1 and the other rows have their maximum at different columns...
This code works perfect to me if I have a matrix of 100x100 for example. But, if the matrix size goes to 50,000x50,000 the code takes to much time in finish it. I now that my code could be the most inneficient way to do it, but I don't know how to deal with this.
I have been reading about threads in python that could help but it doesn't help if I put 50,000 threads because my computer doesn't use more CPU. I also tried to use some functions as .max() but I'm not able to get column of the max an compare it with the other max ...
If anyone could help me of give me a piece of advice to make this more efficient I would be very grateful.
Going to need more information on this. What are you trying to accomplish here?
This will help you get some of the way, but in order to fully achieve what you're doing I need more context.
We'll import numpy, random, and Counter from collections:
import numpy as np
import random
from collections import Counter
We'll create a random 50k x 50k matrix of numbers between -10M and +10M
mat = np.random.randint(-10000000,10000000,(50000,50000))
Now to get the maximums for each row we can just do the following list comprehension:
maximums = [max(mat[x,:]) for x in range(len(mat))]
Now we want to find out which ones are not maximums in any other rows. We can use Counter on our maximums list to find out how many of each there are. Counter returns a counter object that is like a dictionary with the maximum as the key, and the # of times it appears as the value.
We then do dictionary comprehension where the value is == to 1. That will give us the maximums that only show up once. we use the .keys() function to grab the numbers themselves, and then turn it into a list.
c = Counter(maximums)
{9999117: 15,
9998584: 2,
9998352: 2,
9999226: 22,
9999697: 59,
9999534: 32,
9998775: 8,
9999288: 18,
9998956: 9,
9998119: 1,
...}
k = list( {x: c[x] for x in c if c[x] == 1}.keys() )
[9998253,
9998139,
9998091,
9997788,
9998166,
9998552,
9997711,
9998230,
9998000,
...]
Lastly we can do the following list comprehension to iterate through the original maximums list to get the indicies of where these rows are.
indices = [i for i, x in enumerate(maximums) if x in k]
Depending on what else you're looking to do we can go from here.
Its not the speediest program but finding the maximums, the counter, and the indicies takes 182 seconds on a 50,000 by 50,000 matrix that is already loaded.

Resources