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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:46:54+00:00 2026-06-09T08:46:54+00:00

I don’t know if the title is apt, but for the time being this

  • 0

I don’t know if the title is apt, but for the time being this is my question:

I have entries in text file(which has 2 columns) in the following format:

Name     Time  

Santa    1.2
Jim      2.5
Santa    2.7
Santa    2.9

I should form a dictionary which has Name as the key, (Time, Count) as the value. In the above names, Santa repeated 3 times and the time difference of consecutive occurances is less than 2 seconds. So the Count value associated with that entry is 3. If such case occurs, that entry should be deleted from dictionary. Otherwise, count value should be zero (If the 2 occurances of Santa happened 2 seconds apart and 3rd occurance happened after 2 seconds, then count is reinitialized to zero for that entry).

Can this be implemented like this: Make the (Time, Count) as the list and make that list as the values to the keys? I am a newbie to Python, please excuse any mistakes.

The pseudocode is something like this:

Read line in the file:   
    if Santa is in dictionary:    
        time_difference = time from column 2 of the line - dictionary[Santa]  
        if(time_difference < 2):  
            Replace the old occurance with new one along with time  
            # If the previous count value associated with Santa = 1, add 1 to make it 2  
            Make the Count associated with Santa = count+1    
            if(count associated with Santa = 3):  
                delete the Santa entry    
        else:  
            Make count associated with Santa = 1      
    else:  
        Add Santa to dictionary along with its time and make associated count = 1
  • 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-09T08:46:59+00:00Added an answer on June 9, 2026 at 8:46 am

    EDIT: i just noticed you wanted to restart the count timer after 2 seconds of inactivity, i will post that fix in a moment.

    EDIT2: okay, i added it. should be good to go!

    not the cleanest code, but it gets the job done.

    class dictholder():
        def __init__(self):
            self.dict = {}
    
        def add(self, name, time):
            if name in self.dict:
                if (abs(time) - abs(self.dict[name][0]) < 2):
                    self.dict[name][1] += 1
                    self.dict[name][0] = time
                    if self.dict[name][1] == 3:
                        del self.dict[name]
            else:
                self.dict[name] = [time, 1]
            for item in self.dict:
                if (abs(time) - abs(self.dict[item][0]) > 2):
                    self.dict[item][1] = 1
    
        def get(self):
            return self.dict
    

    example:

    d = dictholder()
    d.add("Santa", 1.2)
    d.add("Jim", 2.5)
    d.add("Santa", 2.7)
    d.add("Santa", 2.9)
    
    print d.get()
    
    >>> 
    {'Jim': [2.5, 1]}
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
I don't know if this question is trivial or not. But after a couple
Don't know why but I can't find a solution to this. I have 3
don't know if the title describes anything about what I'm trying to say but
Don't really know how to formulate the title, but it should be pretty obvious
I don't know why, but this code worked for me a month ago... maybe
Don't know if I worded the question right, but basically what I want to
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question
Don't know whats exactly going on, but it's definitely killing my time for nothing.

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.