max() fn has a bug, maybe
April 30 2020 Hello, I hope you and your loved ones are doing well during these tough times which is affecting us all globally. I am independently learning python language and I seemed to have come across a bug. The following is my code: # SWAPPING ELEMENTS i = [10, 3, 8, 7, 4, 12, 6] i [ 0 ] , i [ i.index ( max ( i )) ] = i [ i.index ( max ( i ))] , i [ 0 ] print( i ) i [ -1 ] , i [ i.index ( min ( i ))] = i [ i.index ( min ( i ))] , i [ -1 ] print( i ) # Lines 4 & 5 don't result in a swap even though they're executed. # The same code is written on line 6 & 7 and it gives perfect results # please solve this issue and provide explanation as to why # this occurs. I the above code I intend to swap the largest element in the given list with the first element in the list and similarly the smallest magnitude element with the last element in the given list. Expected result is that the above code will swap on max(i) with i[0] but this does not occur. Surprisingly min(i) and i[-1] works absolutely fine. I am aware that there are ways around it but this seemed intuitive to me and was not resulting in the desired solution, so I decided to mention it to you folks. Stay safe and healthy, Regards, Sami
Hi Sami
I seemed to have come across a bug.
The following is my code:
# SWAPPING ELEMENTS
i = [10, 3, 8, 7, 4, 12, 6]
i [ 0 ] , i [ i.index ( max ( i )) ] = i [ i.index ( max ( i ))] , i [ 0 ] print( i )
This list is not intended to get help about Python, just to report issues about its documentation. Next time you can try stackoverflow, python-help@python.org, or asking on #python on freenode, or reporting the bug to bugs.python.org. For your swap issue, the problem is the execution don't go in the order you first imagined: - Yes, the right hand side of the `=` is fully executed before, no issue here, in other words:
i[0], i[i.index(max(i))] = i[i.index(max(i))], i[0]
is equivalent to:
a, b = i[i.index(max(i))], i[0] i[0], i[i.index(max(i))] = a, b
So the mis-understanding happen in the left-hand-side of the `=` sign: - The first affectation, i[0] = a is done first - Now you're having: [12, 3, 8, 7, 4, 12, 6] - Then the second affectation is done, i[i.index(max(i))] = b Here, max(i) is still 12, but i.index(max(i)) finds it at index 0, so Python does i[0] = 10 - Now you have: [10, 3, 8, 7, 4, 12, 6] While we're at it, i [ i.index ( max ( i ))] should be written: i[i.index(max(i))] (we're still using spaces around `=` and after comas, see PEP8). Oh and please avoid naming lists "i", it's typically used for integers in loops. I know, finding good names is a hard thing. While we're at it, i[i.index(max(i))] is max(i) so your code can be simplified. Now to fix your problem: just don't call i.index twice: >>> i = [10, 3, 8, 7, 4, 12, 6] >>> to_swap = i.index(max(i)) >>> i[0], i[to_swap] = i[to_swap], i[0] >>> i [12, 3, 8, 7, 4, 10, 6] It's shorter, more readable, and it works. Hope it helps, hope you're doing well too. Bests, -- Julien Palard
participants (2)
-
Julien Palard
-
Sami