a=[int(i) for i in input().split()]
b=[]
for i in range(a[0]):
x=[int(i) for i in input().split()]
b.append(x)
print(b)
c=[]
for j in range(len(b)):
c.append(max(b[i]))
print(b[0])
print(c)
2
1 3 45 6 8
2 4 56 7
[[1, 3, 45, 6, 8], [2, 4, 56, 7]]
[1, 3, 45, 6, 8]
[56, 56, 56]
i want to put all the max elements of each list in b to c. but i keep getting the max element of the whole list, while i want max of each list in nested lists which is [45,56]
You have a 2D list and are trying to return a list of the maxes for each element in that 2D list. Iterate over the 2D list and take the max for each element:
res = [max(i) for i in nested_list]
Additionally you can also use map
:
res = list(map(max, nested_list))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments