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.
My code examples are always for Python >=3.6.0
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}')
My code examples are always for Python >=3.6.0
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
  How to make elements return sorted? notsoexperienced 4 764 Sep-24-2020, 09:00 AM
Last Post: perfringo
  Why is my original list also sorted? Pedroski55 1 561 Jul-15-2020, 09:25 PM
Last Post: Yoriz
  Outputting Sorted Text files Help charlieroberrts 1 562 Jul-05-2020, 08:37 PM
Last Post: menator01
  Byte array is sorted when sending via USB daviddlc68 1 1,031 Aug-16-2019, 10:11 AM
Last Post: wavic
  sorted object in list trois 2 1,097 Mar-04-2019, 09:12 AM
Last Post: trois
  understanding sorted key parameter amirt 2 1,393 Jul-30-2018, 06:27 PM
Last Post: amirt
  Reference counting mystery beezergeezer 4 1,589 Jul-24-2018, 02:37 PM
Last Post: beezergeezer
  sorted dictionary? HenryJ 2 1,530 Feb-05-2018, 08:36 AM
Last Post: wavic
  Need some help with a simple syntax mystery kverbeeck 3 1,324 Dec-02-2017, 08:46 PM
Last Post: Windspar
  Help with mystery data paulr 4 2,511 Sep-07-2017, 08:02 AM
Last Post: paulr

Forum Jump:

User Panel Messages

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