简体   繁体   中英

Finding the maximum value per column in each sub-list in python

I'm just wondering how to do the max() and min() when it is a 2 dimensional array. I searched everywhere and I do manage to find something but it is not printing in the corresponding column.

Code:

import random
students = int(input("How many students do you want to enter? \n Students: "))

Student_Count =  [([0] * 4) for i in range(students)]
for a in range(students):
    for b in range(4):
        Student_Count[a][b] = random.randint(1,100)
Max = (list(map(max,Student_Count)))
Min = (list(map(min,Student_Count)))

for a in range (students):
    print("Student", a + 1 , end = "\t\t\t")
    for b in range (4):
        print(Student_Count[a][b], end = "\t\t\t ") # the print of Random numbers
    print()
print("_____________________________________________________________")
print("Highest", end = "\t\t\t\t")
for c in range (4):
    print(Max[c], end = "\t\t\t ")
print()
print("Lowest", end = "\t\t\t\t ")
for c in range (4):
    print(Min[c], end = "\t\t\t ")

output:

How many students do you want to enter? 
 Students: 5
Student 1                       84                       84                      49                      48                    
Student 2                       47                       28                      81                      81                    
Student 3                       89                       31                      95                      79                    
Student 4                       21                       16                      21                      94                    
Student 5                       87                       51                      63                      14                    
_____________________________________________________________
Highest                         84                       81                      95                      94                    
Lowest                           48                      28                      31                      16

EDIT: Sorry for the ambiguous question

My expected outcome for the code is to get the maximum and the minimum value of each column in the list

If what you're aiming for is the minimum of each position, I think the easiest thing is to pull out a sequence of those values and apply min/max to those:

import random

student_count = [
    [random.randint(1, 100) for _ in range(4)]
    for _ in range(int(input(
        "How many students do you want to enter? \nStudents: "
    )))
]

maxima = [max(student[i] for student in student_count) for i in range(4)]
minima = [min(student[i] for student in student_count) for i in range(4)]
sep = "\t"

for a, student in enumerate(student_count):
    print(f"Student {a+1}{sep}{sep.join(map(str, student))}")

print("_____________________________________________________________")
print(f"Highest\t{sep}{sep.join(map(str, maxima))}")
print()
print(f"Lowest\t{sep}{sep.join(map(str, minima))}")
How many students do you want to enter?
Students: 9
Student 1       95      48      19      14
Student 2       37      79      62      93
Student 3       29      73      79      11
Student 4       76      18      9       88
Student 5       39      57      5       4
Student 6       42      92      6       31
Student 7       69      39      70      61
Student 8       16      89      33      76
Student 9       65      54      12      62
_____________________________________________________________
Highest         95      92      79      93

Lowest          16      18      5       4
import random
students = int(input("How many students do you want to enter? \n Students: "))

Student_Count =  [([0] * 4) for i in range(students)]
for a in range(students):
    for b in range(4):
        Student_Count[a][b] = random.randint(1,100)
Max = (list(map(max,Student_Count)))
Min = (list(map(min,Student_Count)))
print(Max)
print(Min)

for a in range(students):
    print("Student", a + 1 , end = "\t\t\t")
    for b in range (4):
        print(Student_Count[a][b], end = "\t\t\t ") # the print of Random numbers
    print()
print("_____________________________________________________________")
print("Highest", end = "\t\t\t\t")
for c in range(students):
    print(Max[c], end = "\t\t\t ")
print()
print("Lowest", end = "\t\t\t\t ")
for c in range(students):
    print(Min[c], end = "\t\t\t ")

Output: 这是输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM