Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7996359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:41:27+00:00 2026-06-04T14:41:27+00:00

I have three numeric values (weight, count, contribution) for various strings (words) that i

  • 0

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))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:41:28+00:00Added an answer on June 4, 2026 at 2:41 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dependent UIPicker that loads the values into its two components from
I have a couple of text boxes that accept numeric values. When a user
I have a combo box CBS_DROPDOWNLIST control filled with numeric values. I'd like for
I have several strings which contain numberic values. I'd like to format these strings
I have a table with one numeric value (n) and three string values (a,b,c).
I have a list that contains groups of nearly identical numeric values. i.e. (1004.523,
I have a SQL varchar column that has values like 100, 2000 and S5000.
We have a piece of equipment that outputs its GPS coordinates as numeric values
I have a list of key/value pairs and would like to convert it into
I have a list of items that has numeric values and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.