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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:05:32+00:00 2026-06-03T06:05:32+00:00

I have a file having a few columns like: PAIR 1MFK 1 URANIUM 82

  • 0

I have a file having a few columns like:

PAIR 1MFK 1 URANIUM 82 HELIUM 112 2.5506  
PAIR 2JGH 2 PLUTONIUM 98 POTASSIUM 88 5.3003  
PAIR 345G 3 SODIUM 23 CARBON 14 1.664  
PAIR 4IG5 4 LITHIUM 82 ARGON 99 2.5506  
PAIR 234G 5 URANIUM 99 KRYPTON 89 1.664  

Now what I wanted to do is read the last column and iterate the values for repetitions and generate an output file containing two column 'VALUE' & 'NO OF TIMES REPEATED'.

I have tried like:

inp = ('filename'.'r').read().strip().replace('\t',' ').split('\n')
from collections import defaultdict
D = defaultdict(line)

for line in map(str.split,inp):
     k=line[-1]
     D[k].append(line)

I’m stuck here.
plaese help.!

  • 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-03T06:05:35+00:00Added an answer on June 3, 2026 at 6:05 am

    There are a number of issues with the code as posted. A while-loop isn’t allowed inside a list comprehension. The argument to defaultdict should be list not line. Here is a fixed-up version of your code:

    from collections import defaultdict
    D = defaultdict(list)
    
    for line in open('filename', 'r'):
        k = line.split()[-1]
        D[k].append(line)
    
    print 'VALUE    NO TIMES REPEATED'
    print '-----    -----------------'
    for value, lines in D.items():
        print '%-6s           %d'  % (value, len(lines))
    

    Another way to do it is to use collections.Counter to conveniently sum the number of repetitions. That let’s you simplify the code a bit:

    from collections import Counter
    D = Counter()
    
    for line in open('filename', 'r'):
        k = line.split()[-1]
        D[k] += 1
    
    print 'VALUE    NO TIMES REPEATED'
    print '-----    -----------------'
    for value, count in D.items():
        print '%-6s           %d'  % (value, count)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file log that I would like to parse and am having
I am having few excel files where each file have multiple workbooks. Assume following
I have a File having text and few numbers.I just want to extract numbers
I have a text file having words that i need to validate. After following
I have a .txt file having some text. I want to read this file
I have a flash file menu i am having problem while link it to
I am having utf-8 encoded file containing arabic text and I have to search
I am having following issue with reading binary file in C. I have read
I have a *.txt files having integers, one on each line. So the file
Hi I am having trouble getting an embedded js file to work. I have

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.