Suppose
A = [9, 5, 34, 33, 32, 31, 300, 30, 3, 256]
I want to sort only a particular section in a list. For example, here I want to sort only [300, 30, 3]
so that overall list becomes:
A = [9, 5, 34, 33, 32, 31, 3, 30, 300, 256]
Suppose B = [300, 30, 400, 40, 500, 50, 600, 60]
then after sorting it should be B = [30, 300, 40, 400, 50, 500, 60, 600]
.
Main idea if the leftmost digit is same 300, 30, 30
and right most digits contain only zeros
then we should arrange it in increasing order.
Another example:
A = [100, 10, 1, 2000, 20, 2]
After sorting it should be A = [1, 10, 100, 2, 20, 2000]
Could anyone suggest some techniques to approach such issue. The values in my list will always be arranged in this way [200, 20, 2, 300, 30, 3, 400, 40, 4]
.
Code:
nums = [3, 30, 31, 32, 33, 34, 300, 256, 5, 9]
nums = sorted(nums, key=lambda x: str(x), reverse=True)
print nums
>> [9, 5, 34, 33, 32, 31, 300, 30, 3, 256]
But my final output should be [9, 5, 34, 33, 32, 31, 3, 30, 300 256]
.
Here is a big example:
A = [9, 5, 100, 10, 30, 3, 265, 200, 20, 2]
After sorting it should be:
A = [9, 5, 10, 100, 3, 30, 265, 2, 20, 200]