Python Forum
sorted function example mystery
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorted function example mystery
#1
The following code works, but I don't know why (I'm using python v 3.7.3)

print(f'{"".join(sorted("foo", key="of".index))}')
It correctly prints oof

I don't understand:
key="of".index
Reply
#2
"of".index is the index function of str.
It return the index of an element. The function takes an element, which must be inside the str.
If not, it will raise a ValueError.

The key function is called for each char in the str (str is iterable).
The sort order is defined as follows: o == 0 and f == 1
This means, that after sorting, the o comes first, then the f.

It's a strange way to get a custom defined sorting order for str.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
To illustrate DeaD_Eye answer with code:

>>> 'of'.index('f')
1
>>> 'of'.index('o')
0
>>> 'of'.index('a')
/.../
ValueError: substring not found
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Thanks guys. I'm studying Python by working through coding problems from codewars.com
It's a good site as there are many code examples of the same problem to look at.
Reply
#5
This example snippet you've posted is an Anti-pattern.
The sense behind f-strings are to make it easier readable and not to save lines of code.

def my_keyfunc(char):
    """
    Return the index of the string "of"
    """
    return "of".index(char)

sorted_str = "".join(sorted("foo", key=my_keyfunc))
print(f'This is the sorted_str: {sorted_str}')
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failing to print sorted files tester_V 4 1,188 Nov-12-2022, 06:49 PM
Last Post: tester_V
  set and sorted, not working how expected! wtr 2 1,254 Jan-07-2022, 04:53 PM
Last Post: bowlofred
Shocked An array "mystery": The same array, the same operations but different outcomes mewss 3 2,113 Feb-17-2021, 06:34 PM
Last Post: mewss
  How to make elements return sorted? notsoexperienced 4 3,164 Sep-24-2020, 09:00 AM
Last Post: perfringo
  Why is my original list also sorted? Pedroski55 1 1,581 Jul-15-2020, 09:25 PM
Last Post: Yoriz
  Outputting Sorted Text files Help charlieroberrts 1 1,684 Jul-05-2020, 08:37 PM
Last Post: menator01
  Byte array is sorted when sending via USB daviddlc68 1 2,776 Aug-16-2019, 10:11 AM
Last Post: wavic
  sorted object in list trois 2 2,209 Mar-04-2019, 09:12 AM
Last Post: trois
  [Help] sorted() in while loop with user's input() {Screenshot attached} vanicci 5 3,952 Aug-04-2018, 08:59 PM
Last Post: vanicci
  understanding sorted key parameter amirt 2 2,720 Jul-30-2018, 06:27 PM
Last Post: amirt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020