Reputation: 111
Need some help on a quick task i need to complete.
I have a dictionary with the following subset of values...
{249863: [1, 'Sr. Financial Analyst'], 180241: [1, 'C# .NET Developer - SQL/Databases, Visual Studio 2010, WPF'], 981011: [1, 'Sr. Business Systems Analyst'], 581669: [1, 'Pharmaceutical Sales - Intern'], 641064: [1, 'Software Engineer II -'], 708653: [1, ' Sports Minded Consultant - Sales - Management - Entry Level'], 41011: [1, 'Regional Revenue Manager- West Coast'], 81979: [1, 'Assistant Buyer'], 401479: [2, 'Valve Technician'], 876619: [1, 'Manufacturing Engineer'],
The Dictionary is structred with KEY: ID Number, Values: Count, Title I need to organize this dictionary and print the top 5 keys based on COUNT.
Currently I have came up with this code, but it does not seem to be doing what I need:
problem2 = dict(sorted(task2.iteritems(), key=operator.itemgetter(1), reverse=True)[:5])
Currently problem2 prints:
{627329: [1, 'Young Professionals Customer Relations'], 547457: [1, 'iPhone/Android/Mobile OS Apps Developer'], 520659: [1, 'technician/receptionist'], 178789: [2, 'Business Planning Analyst'], 401479: [2, 'Valve Technician']}
But as you can see its printing some 1's first.
Thanks for all help in advance!, Please remember, I need to keep it working for Python 2.4, not 2.7 :( Constraint.
Upvotes: 1
Views: 1161
Reputation: 26727
Don't use a dict, convert it to a list of tuples using dict.items()
then sort that.
>>> a = {'x': [2, 'apple'], 'y': [3, 'pear'], 'w': [5, 'banana']}
>>> list(a.items())
[('y', [3, 'pear']), ('x', [2, 'apple']), ('w', [5, 'banana'])]
>>> sorted(a.items(), key=lambda x: x[1][0])
[('x', [2, 'apple']), ('y', [3, 'pear']), ('w', [5, 'banana'])]
Alternatively you could create a sorted list of keys into the dictionary and use that to look up the top/bottom key-value pairs as needed.
>>> a_order = sorted(a.keys(), key=lambda x: a[x][0])
>>> a_order
['x', 'y', 'w']
Upvotes: 1
Reputation: 5193
By definition dicts are unordered data-structures. If you want to guarantee an order, you must use OrderedDict
>>> from collections import OrderedDict
>>> problem2 = OrderedDict(sorted(task2.iteritems(), key=operator.itemgetter(1), reverse=True)[:5])
>>> problem2
OrderedDict([(401479, [2, 'Valve Technician']), (249863, [1, 'Sr. Financial Analyst']), (981011, [1, 'Sr. Business Systems Analyst']), (641064, [1, 'Software Engineer II -']), (41011, [1, 'Regional Revenue Manager- West Coast'])])
>>>
If you are using older version of python, I'm afraid that you must keep the order manually, so you must maintain an ordered list of keys and then access to original dict
>>> sorted_keys = [i[0] for i in sorted(task2.iteritems(), key=operator.itemgetter(1), reverse=True)[:5]]
>>> task2[sorted_keys[0]]
[2, 'Valve Technician']
Upvotes: 2
Reputation: 239573
Normal dictionaries don't maintain order of the elements in them. So, you need to use collections.OrderedDict
, like this
problem2 = collections.OrderedDict(sorted(task2.iteritems(), key=operator.itemgetter(1), reverse=True)[:5])
Upvotes: 1