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

  • Home
  • SEARCH
  • 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 8629407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:47:52+00:00 2026-06-12T08:47:52+00:00

The collections.Count.most_common function in Python uses the heapq module to return the count of

  • 0

The collections.Count.most_common function in Python uses the heapq module to return the count of the most common word in a file, for instance.

I have traced through the heapq.py file, but I’m having a bit of trouble understanding how a heap is created/updated with respect to words let’s say.

So, I think the best way for me to understand it, is to figure out how to create a heap from scratch.

Can someone provide a pseudocode for creating a heap that would represent word count?

  • 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-12T08:47:53+00:00Added an answer on June 12, 2026 at 8:47 am

    this is a slightly modified version of the code found here : http://code.activestate.com/recipes/577086-heap-sort/

    def HeapSort(A,T):
        def heapify(A):
            start = (len(A) - 2) / 2
            while start >= 0:
                siftDown(A, start, len(A) - 1)
                start -= 1
    
        def siftDown(A, start, end):
            root = start
            while root * 2 + 1 <= end:
                child = root * 2 + 1
                if child + 1 <= end and T.count(A[child]) < T.count(A[child + 1]):
                    child += 1
                if child <= end and T.count(A[root]) < T.count(A[child]):
                    A[root], A[child] = A[child], A[root]
                    root = child
                else:
                    return
    
        heapify(A)
        end = len(A) - 1
        while end > 0:
            A[end], A[0] = A[0], A[end]
            siftDown(A, 0, end - 1)
            end -= 1
    
    
    if __name__ == '__main__':
        text = "the quick brown fox jumped over the the quick brown quick log log"
        heap = list(set(text.split()))
        print heap
    
        HeapSort(heap,text)
        print heap
    

    Output

    ['brown', 'log', 'jumped', 'over', 'fox', 'quick', 'the']
    ['jumped', 'fox', 'over', 'brown', 'log', 'the', 'quick']
    

    you can visualize the program here
    http://goo.gl/2a9Bh

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

Sidebar

Related Questions

In Python 2.7, I want to iterate over a collections.Counter instance in descending count
I have the following method: public bool IsValid { get { return (GetRuleViolations().Count() ==
I have a collections.defaultdict(int) that I'm building to keep count of how many times
I'm running into a common need in my project to return collections of my
I have a small python script that prints the 10 most frequent words of
private void PersonalInfoList_SelectionChanged(object sender, SelectionChangedEventArgs e) { System.Collections.IList list = e.AddedItems; if (e.AddedItems.Count ==
for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}
I have 2 collections, one of available features and one of user features. I
I have a system.collections.generic.list(of ListBox) I would like to use the collection classes built-in
I have 2 IEnumerable collections. IEnumerable<MyClass> objectsToExcept and IEnumerable<MyClass> allObjects. objectsToExcept may contain objects

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.