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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:14:21+00:00 2026-06-04T15:14:21+00:00

I created a dictionary of lists using: names = defaultdict(list) I am reading a

  • 0

I created a dictionary of lists using:

names = defaultdict(list)

I am reading a line from the text file, splitting it into 3 columns. I am making the first column of the line as key and other two columns as the values in a list. I used the following code segment as following:

with open('f.txt') as f:
    lines = [l.split() for l in f.readlines() if l.strip()]
    for l in lines:
            names[l[0]].append([l[1],l[2]])

I have the text files like this:

00:12:34:23:45:67       134     123.456

45:34:23:45:44:23       133     345.123

3f:32:dr:45:34:r5       133     212.345

00:12:34:23:45:67       134     555.555

00:12:34:23:45:67       136     555.556

00:12:34:23:45:67       134     555.560

If i run my program to output key:value pairs, the output is:

00:12:34:23:45:67 [['134', '123.456'], ['134', '555.555'], ['136', '555.556'], ['134', '555.560']]

45:34:23:45:44:23 [['133', '345.123']]

3f:32:dr:45:34:r5 [['133', '212.345']]

How can I access the individual elements of the list? For example, in this output, I want to count number of 133, 134, 135, 136 of each key and the time difference between each type. For example, for the key 00:12:34:23:45:67, there are 3 134’s, 1 136. The time difference between highest of and lowest of 134’s for that key is 555.560-123.456. I want this result for all 133,134,135,136 types. I am struggling for this problem from many days. please help. Much appreciated.

  • 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-04T15:14:23+00:00Added an answer on June 4, 2026 at 3:14 pm

    It seems like a better data structure is in order here…

    from collections import defaultdict
    names=defaultdict(lambda :defaultdict(list))
    
    with open('f.txt') as f:
       lines = [l.split() for l in f.readlines() if l.strip()]
       for l in lines:
           names[l[0]][l[1]].append(float(l[2]))
    

    Now, to get the counts and differences from max/min:

    for k,v in names.items():
        for kk,vv in v.items():
            print k,kk,len(vv),max(vv)-min(vv)
    

    Output:

    00:12:34:23:45:67 136 1 0.0
    00:12:34:23:45:67 134 3 432.104
    45:34:23:45:44:23 133 1 0.0
    3f:32:dr:45:34:r5 133 1 0.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a dictionary from 2 lists where one list contains
I am trying to implement vectorization of a text file...I have created a dictionary
I'm using os.listdir and a file to create a dictionary. I'm getting keys and
I have created a list of dictionary in a format as below: [{u'PLAN_A': True,
I have created a dictionary in Class A and would like to return the
I am trying to created nested dictionary variable like the below, But I get
I have defined a class Listener and created a dictionary of Listener objects. Each
I'm a C++ expert, but not at all for C#. I created a Dictionary<string,
I created a class Entry to handle Dictionary entries, but in my main(), I
I have a Dictionary of objects I have created in smalltalk, which I am

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.