Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSS

PY Bisect key sort

import bisect
import operator

p1 = {"id": 11, "name": "Diane"}
p2 = {"id": 12, "name": "Bob"}
p3 = {"id": 13, "name": "Emma"}

key = operator.itemgetter("name")
people = sorted([p1, p2, p3], key=key)
# Bob, Diane, Emma

idx = bisect.bisect(people, "Dan")
# TypeError: '<' not supported between instances of 'str' and 'dict'

idx = bisect.bisect(people, "Dan", key=key)
# idx == 1
Source by antonz.org #
 
PREVIOUS NEXT
Tagged: #PY #Bisect #key #sort
ADD COMMENT
Topic
Name
8+6 =