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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:13:02+00:00 2026-06-09T04:13:02+00:00

This should be a simple problem but I just can’t wrap my head around

  • 0

This should be a simple problem but I just can’t wrap my head around it. I have a dictionary called TD. The {key1{key2:values}} of TD are {1:{u'word':3, u'next':2, u'the':2},2:{...}...} where key1 is a document,key2 is a word in a document and value is the number of times that word appears in the document, obtained using the Counter method.

I have a large number of documents so each document has an entry in TD:

TD = {1:{u'word':2, u'next':1, u'the':5,...},
      2:{u'my':4, u'you':1, u'other':2,...},
      ...
      168:{u'word':1, u'person':1, u'and':8,...}}

What I now want to do is check each word in {1{...}} to see if it appears in the other documents and repeat this process for each document. For each document a word appears in, freq is increased by 1. I have a new dictionary called Score that should look like this:

{1:{u'word':score, u'next':score,...}, 2:{u'my':score, u'you':score,...}...}

To obtain this dictionary:

Score={}
count = 0
for x,i in TD[count].iteritems():
    freq=1
    num=1
    for y in TD[num].keys():
        if word in TF[num].keys():
            freq+=1
        num+=1
    Score[num]={x:(i*freq)}
    num+=1

This is giving me the following output:

{1:{u'word':score}, 2:{u'next':score}, 3:{u'the':score}...}

should be:

{1:{u'word':score, u'next':score, u'the':score,...}...}

I think the problem is with the line Score[num]={x:(i*freq)}

  • 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-09T04:13:04+00:00Added an answer on June 9, 2026 at 4:13 am

    Use dict views to find the intersection between documents, then a Counter to count their frequencies:

    Score = {}
    for id, document in TD.iteritems():
        counts = Score[id] = Counter()
        for otherid, otherdocument in TD.iteritems():
            if otherid == id:
                continue  # Skip current document
            counts.update(document.viewkeys() & otherdocument.viewkeys())
    

    Each entry in Score will be a count of how often each word in a document appears in the other documents.

    If you need to include the word count in the current document as well (count + 1), simply remove the if otherid == id test.

    In your own code you confused num and count, but in python you usually don’t need to manually increment a loop counter in any case.

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

Sidebar

Related Questions

This should be a simple problem, but I just can't seem to find a
i guess this is a really simple problem, but i just can't get it
This should be a simple problem, but I am have a tough time getting
This should probably have a simple answer I just can't figure out. Anyway, I
This problem is very simple, but I just can't figure it out added to
Hi all I have a (probably) simple problem but I just can't figure out
this should be really simple but I can just not figure it out at
This should be simple, but I'm getting confused. I have a parent/child tables -
This should seem simple enough, but can't figure it out. I was porting a
This should be simple, yet I can't get it to work. I have a

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.