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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:26:42+00:00 2026-06-12T23:26:42+00:00

In the sample data given below (stored in a file), I need to find

  • 0

In the sample data given below (stored in a file), I need to find distinct ‘ids’ in each ‘item’ category in the fastest way possible. I can do this by going through each line and then finding all item sets and then count, but I am looking for a faster method such as ‘Counter’ or ‘itemgetter’.

“infile.txt”

id  item
444 Anemia
444 liver
444 Anemia
444 Anemia
222 liver
222 pancreas
222 liver
222 Anemia
444 pancreas
444 pancreas
444 Anemia
001 Iiver
001 pancreas
111 pancreas
111 liver
111 liver
111 pancreas
555 pancreas
555 liver
555 pancreas
555 liver
555 pancreas
555 liver

I need the output something like the following

item    count   ids
pancreas    5   001, 111, 222, 444, 555
liver   5   111,222,444,555,001
Anemia  2   222,444
  • 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-12T23:26:43+00:00Added an answer on June 12, 2026 at 11:26 pm

    I’d use a defaultdict with a set

    from collections import defaultdict
    d = defaultdict(set)
    with open(datafile) as f:
        for line in f:
            my_id,item = line.split()
            d[item].add(my_id)
    
    for item in d:
        print item,len(d[item]),sorted(d[item])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate 3-day correlation. A sample matrix is given below. My problem
I am having a sample file as given below. This is an SQL Loader
I have hierarchical data stored in an XML file. There are multiple companies, each
I am trying to sample a data frame from a given data frame such
I have below sample data. AID Date Title ----- ---------- ------ 1 2011-12-12 test1
I am getting JSon data from a web service, the sample data is given
Please find xml data below. I would like to obtain nodes with keys Sub1
Given this sample data set: Date_Time 10/1/2011 12:05:00 AM 10/6/2011 2:25:00 AM 10/3/2011 5:59:00
Given a simple data set, I would like to be able to calculate a
Given the following simple BST definition: data Tree x = Empty | Leaf x

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.