Hello everyone here is my code:
n =[[34,2,55,24,22],[31,22,4,7,333],[87,74,44,12,48]]
for r in n:
for c in r:
print(c,end = " ")
print()
sums=[]
for i in n:
sum=0
for num in i:
sum =int(num)
sums.append(sum)
print(*sums)
print(*(min(row) for row in n))
And here is what it prints out:
34 2 55 24 22
31 22 4 7 333
87 74 44 12 48
137 397 265
2 4 12
I need to change row whith smallest number and bigest number so it means row 1 and 2 like this:
31 22 4 7 333
34 2 55 24 22
87 74 44 12 48
#end result needs to look like this:
34 2 55 24 22
31 22 4 7 333
87 74 44 12 48
137 397 265
2 4 12
31 22 4 7 333
34 2 55 24 22
87 74 44 12 48
Please help me i cant use numpy because it doesnt work I tried using it but all it gives are errors.
CodePudding user response:
I assume you want the list with max at the first index and the one with the min at the end,
maxs = [max(i) for i in n]
mins = [min(i) for i in n]
max_idx = maxs.index(max(maxs))
min_idx = mins.index(min(mins))
n[max_idx], n[min_idx] = n[min_idx], n[max_idx]
# you need to think about when min_idx = max_idx
# or when there's more than one max/min
If you don't mind numpy
, you can use:
max_idx = np.argmax(np.max(n, axis=1))
min_idx = np.argmin(np.min(n, axis=1))