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 8952185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:49:12+00:00 2026-06-15T13:49:12+00:00

Given a histogram as a dictionary, what is the most pythonic, batteries included way

  • 0

Given a histogram as a dictionary, what is the most pythonic, “batteries included” way of sorting a list which only has elements from the dictionary, by the frequencies defined in that dictionary?

The keys of the dictionary (and implicitly the values in the list) are strings, and the frequencies are stored as integers.

I am interested only in the python2 solution, but you’re welcome to write a python solution as well, so others can benefit from it too (in the future).

  • 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-15T13:49:14+00:00Added an answer on June 15, 2026 at 1:49 pm
    >>> inList = [1,2,3,4,5]
    >>> inDict = {1:5, 2:2, 3:4, 4:1, 5:3}
    >>> sorted(inList, key=lambda x: inDict.get(x,0))
    [4, 2, 5, 3, 1]
    

    This also has the benefit of sorting elements not in the dict as if it was in the dict with a value of 0, instead of just raising a KeyError

    The sorted() function has an optional argument ‘key’. This argument specifies a function of one argument that is used to extract a comparison key from each list element. This comparison key is used to determine ordering between elements.

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

Sidebar

Related Questions

I want to remove elements (histogram bins) from an std::unordered_map (histogram) that fulfills a
A Google Collections Multiset is a set of elements each of which has a
I'll start the ball rolling. Given a sequence of elements, some of which can
Given a generated histogram that I got from an image I was wondering if
Proceeding from this post on histogramming, is there a way to override the histogram
I am trying to write a python program that calculates a histogram, given a
Given an NSMutableArray of dynamic CGPoint s, what is the fastest and most efficient
Given the jQuery dropdown plugin below. Is there a way to add a method
Given following Table select rv, mv, datetime, rawv from mytable where datetime > '2012-01-30';
The expected probability of randomly selecting an element from a set of n elements

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.