The following code produces inconsistent results if executed multiple times. I ran it on Debian 8.4 (jessie) with Python-3.5.1.
from heapq import nlargest
from operator import itemgetter
dd = dict([('41', 768.0), ('2', 15275.0), ('9', 1728.0), ('90', 1728.0),
('97', 1200.0), ('68', 2904.0), ('98', 4380.0), ('16', 768.0),
('37', 768.0), ('17', 1587.0), ('25', 4495.4)])
print(nlargest(5, dd.items(), key=itemgetter(1)))
outputs after multiple execution:
[('2', 15275.0), ('25', 4495.4), ('98', 4380.0), ('68', 2904.0), ('90', 1728.0)]
[('2', 15275.0), ('25', 4495.4), ('98', 4380.0), ('68', 2904.0), ('9', 1728.0)]
[('2', 15275.0), ('25', 4495.4), ('98', 4380.0), ('68', 2904.0), ('90', 1728.0)]
[('2', 15275.0), ('25', 4495.4), ('98', 4380.0), ('68', 2904.0), ('90', 1728.0)]
It looks random, can anybody explain why this is happening?
if replace dd.items()
with sorted(dd.items())
, then the output becomes deterministic. i.e.
[('2', 15275.0), ('25', 4495.4), ('98', 4380.0), ('68', 2904.0), ('9', 1728.0)]
I also tried above code on OSX and CentOS6.7 with python-2.7, and it always returns
[('2', 15275.0), ('25', 4495.4), ('98', 4380.0), ('68', 2904.0), ('9', 1728.0)]
Can this be a bug in Python3 heapq
implementation?