3

I have three numeric values (weight, count, contribution) for various strings (words) that i would like to organise into one multidimensional array, and then sort. To do this, I made lists within a dictionary, where the numeric values are in the list and the string is the key:

print_dictionary[word] = [weight,count,contribution]

How can I sort, first in ascending order and then in descending order, by 'contribution' (the third value in the list), and show the first 10 items of the sorted list. How can I do this?

For example, for the following print_dictionary:

print_dictionary[sam] = [2,7,1]
print_dictionary[sun] = [4,1,3]
print_dictionary[dog] = [1,3,2]

I want to them be able to sort contribution in ascending order:

Word:   Weight:   Count:    Contribution:
sam     2         7         1
dog     1         3         2
sun     4         1         3

I don't see how itemegetter can be used for this:

sorted(print_dictionary, key=itemgetter(2))
6
  • Lists are ordered in python. I think you meant "dicts are unordered". Commented May 27, 2012 at 2:48
  • thanks, I edited the post to clear that up. Commented May 27, 2012 at 2:52
  • Also, you can't use itemgetter on a dictionary like that. You have to call print_dictionary.items(). Commented May 27, 2012 at 2:53
  • Do you just want the keys in order so that you can access it in contribution order? Commented May 27, 2012 at 3:09
  • I want to sort between sets, not within sets. I added an example in the question. Commented May 27, 2012 at 3:16

2 Answers 2

4

You can pass an anonymous function as the key to sorted. This uses the third member of the multi-dimensional dict as the key:

>>> d = {'a': [1, 4, 7], 'b': [2, 3, 9], 'c': [3, 2, 8]}
>>> for key in sorted(d, key=lambda x: d[x][2]):
...    print key, d[key]
a [1, 4, 7]
c [3, 2, 8]
b [2, 3, 9]

For descending order, use reverse=True. To limit the results, add [:N]:

sorted(d, key=lambda x: d[x][2], reverse=True)[:2]

# b [2, 3, 9]
# c [3, 2, 8]

More about sorted and sorting in Python.

Sign up to request clarification or add additional context in comments.

1 Comment

Close, just put the slice statement on sorted: sorted(d, key=lambda x: d[x][2])[:10]
1

You can't really sort a dictionary; when you try, you are really just sorting the list of keys from the dictionary. You can do that using a custom sort comparison that looks at the third item in the value.

sorted(print_dictionary, key=lambda word: print_dictionary[word][2])

So to generate your report, something like this would work:

sorted_keys = sorted(print_dictionary, key=lambda word: print_dictionary[word][2])

print "Word:\tWeight:\tCount:\tContribution"      
for i in range(10): # or however many you want
    word = sorted_keys[i]
    values = print_dictionary[word]
    print "\t".join([word]+[str(n) for n in values])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.