This is working well with python2 the 2nd argument type change from str to dict sometimes but that'ok
With pyton3 i have this TypeError: '<' not supported between instances of 'dict' and 'str'
I have seen this TypeError: '<' not supported between instances of 'tuple' and 'str' that understand and heapq with custom compare predicate with a good answer :
...The functions in the heapq module are a bit cumbersome (since they are not object-oriented), and always require our heap object (a heapified list) to be explicitly passed as the first parameter. We can kill two birds with one stone by creating a very simple wrapper class that will allow us to specify a key function, and present the heap as an object....
but a lot complicated to create a new class.
Also https://docs.python.org/3.6/library/heapq.html in 8.5.2. Priority Queue Implementation Notes section but i want working with tuple(int, str) or tuple(int, dic).
I have seen too How to use lambdas in python heapq? seems be good ?
And other interesting ways are How to make heapq evaluate the heap off of a specific attribute? and In Python, heapq.heapify doesn't take cmp or key functions as arguments like sorted does
Is anybody here ? (Jim)
This example programme bug in python3 comes from http://python-prepa.github.io/information_theory.html
occurrences={'A': 5, 'R': 2, 'B': 2, 'C': 1, 'D': 1}
tas = [(occ, lettre) for (lettre, occ) in occurrences.items()]
heapify(tas)
while len(tas) >= 2:
print (tas)
occ1, noeud1 = heappop(tas)
occ2, noeud2 = heappop(tas)
# print(type(noeud1))
# print(type(noeud2))
t=(occ1 + occ2, {0: noeud1, 1: noeud2})
# print(occ1,noeud1)
# print(occ2,noeud2)
# print(t)
heappush(tas, t)
# heappush(tas, (occ1 + occ2, {0: noeud1, 1: noeud2}))
print (tas)