-4

I made a absolute function to print absolute values in python programming but its not returning values that what i expected.

l1=[1,2,3,4,5,-1,-5,-9,10]
def absolute(x):
    if x>=0:
        return x
    else:
        return -x
print(sorted(l1, key=absolute))

output: [1, -1, 2, 3, 4, 5, -5, -9, 10] Expected Output: [1, 1, 2, 3, 4, 5, 5, 9, 10]

Please help me fast if any can??

Gambler Aziz
  • 41
  • 1
  • 10
  • 3
    You sorted the *original* values *by* their absolute values. Preserving the original values is the whole reason `key` exists. – user2357112 May 17 '20 at 06:35
  • yes, thats what sorted() function does. It preserves original values. – Gambler Aziz May 17 '20 at 06:37
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – Patrick Artner May 17 '20 at 07:36

3 Answers3

3

The function does exactly what it's supposed to: sorts the elements by their absolute value. |3| < |-5| so 3 will go before -5. If you want the list to contain the sorted absolute values, you need to change it first:

output = sorted(map(abs, [1, -1, 2, 3, 4, 5, -5, -9, 10]))

(Notice that abs already exists as a builtin function).

bereal
  • 32,519
  • 6
  • 58
  • 104
  • but abs function suppose to return values in positive why it is showing me in negative. – Gambler Aziz May 17 '20 at 06:45
  • 4
    @GamblerAziz What `key` function returns is only used to _compare_ values while sorting. It does not replace the original values in the list. – bereal May 17 '20 at 06:46
2

You are using your function as key in sorted which only sorts the list. If you want to apply the function on every argument you can use map.

This is for you example but better is use abs

l1=[1,2,3,4,5,-1,-5,-9,10]
def absolute(x):
    if x>=0:
        return x
    else:
        return -x
print(sorted(map(absolute, l1)))
Martin Nečas
  • 593
  • 2
  • 9
  • I think every time "key" passes each values to the absolute function. and here you didn't used sorted function. – Gambler Aziz May 17 '20 at 06:40
  • 1
    It compares the values which are returned and then sort them by it. Ii does not change the original value only sorts them. – Martin Nečas May 17 '20 at 06:43
0

When you sort a list you have the items of the list and a way to compare them. If you don't specify a key the compare will be of their actual values with integer comparative. When you do specify a key the sort algorithm will compare key(x) to key(y) instead of x to y in order to determine which should be first. Anyway, in order to do what you want, you need to replace each element by its abs value

print(sorted([abs(item) for item in l1])

(You don't need to define your own absolute function but it would work the same way if you will use absolute(item)

Note that here you don't need to use key because the list is comparing the items that are already in absolute value.

Tomer Shinar
  • 415
  • 3
  • 13