Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
C0D4681385yMake a second list and sort that so you can retain the original, or what @p100sch said.
-
@tits-r-us
nums = [4,3,5]
nums1 = sorted(nums)
#nums1 = [3,4,5]
maxElement = nums1[len(nums1)-1]
for i in range(len(nums)):
if nums[i] == maxElement
oldIndex = i -
But the above logic fails when there are duplicate elements as this logic will give the last found element from the list.
eg list = [1,3,2,3] -
bahua129045ySounds like homework for a CS course. If it is, it's intended to get you thinking in the frame of mind of these objects and structures. A huge amount of the value of a good programmer is the ability to come up with creative solutions to unexpected problems. Asking for, "the answer" on an online forum ignores that.
-
@korrat
https://leetcode.com/problems/...
I was trying to solve the above problem which requires that.
So my approach requires sorting the list first but that leads to change in indexes of list elements, which creates a problem. -
korrat6345y@ishank-dev in that case you could map the numbers to pairs of index and number and then sort by number.
Related Rants
Does anyone know a good way to retrieve back the indexes of elements in the unsorted list after sorting that list in python?
Let's say for example I want to find the largest element in a list, so I sort it and find the last element(as it will be the largest), now I want to retrieve its original position in the unsorted list.
question
python