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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:43:28+00:00 2026-05-13T20:43:28+00:00

I have two files which I loaded into lists. The content of the first

  • 0

I have two files which I loaded into lists. The content of the first file is something like this:

d.complex.1
23
34
56
58
68
76
.
.
.
etc
d.complex.179
43
34
59
69
76
.
.
.
etc

The content of the second file is also the same but with different numerical values. Please consider from one d.complex.* to another d.complex.* as one set.

Now I am interested in comparing each numerical value from one set of first file with each numerical value of the sets in the second file. I would like to record the number of times each numerical has appeared in the second file overall.

For example, the number 23 from d.complex.1 could have appeared 5 times in file 2 under different sets. All I want to do is record the number of occurrences of number 23 in file 2 including all sets of file 2.

My initial approach was to load them into a list and compare but I am not able to achieve this. I searched in google and came across sets but being a python noob, I need some guidance. Can anyone help me?

If you feel the question is not clear,please let me know. I have also pasted the complete file 1 and file 2 here:

http://pastebin.com/mwAWEcTa
http://pastebin.com/DuXDDRYT

  • 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-05-13T20:43:29+00:00Added an answer on May 13, 2026 at 8:43 pm

    First create a function which can load a given file, as you may want to maintain individual sets and also want to count occurrence of each number, best would be to have a dict for whole file where keys are set names e.g. complex.1 etc, for each such set keep another dict for numbers in set, below code explains it better

    def file_loader(f):
        file_dict = {}
        current_set = None
        for line in f:
            if line.startswith('d.complex'):
                file_dict[line] = current_set = {}
                continue
    
            if current_set is not None:
                current_set[line] = current_set.get(line, 0)
    
        return file_dict
    

    Now you can easily write a function which will count a number in given file_dict

    def count_number(file_dict, num):
        count = 0
        for set_name, number_set in file_dict.iteritems():
            count += number_set.get(num, 0)
    
        return count
    

    e.g here is a usage example

    s = """d.complex.1
    10
    11
    12
    10
    11
    12"""
    
    file_dict = file_loader(s.split("\n"))
    print file_dict
    print count_number(file_dict, '10')
    

    output is:

    {'d.complex.1': {'11': 2, '10': 2, '12': 2}}
    2
    

    You may have to improve file loader, e.g. skip empty lines, convert to int etc

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

Sidebar

Related Questions

I have two CSV files which use @ to divide each column. The first
I have a web application and two class files, First class is MyClass.class which
I have two files: A sample XML file. A .xsd file w/ schema, which
I have two files which both contain a list of words. Is there an
I have two ocx files which i registered on my computer using regsvr32. I
I have two batch files which is used to run a large C++ build,
I have two files called file_utils.h and file_utils.cpp which contain some methods and variables
I have the following code which will generate two pdf files containing plots to
I have two string variables which are both file paths. The code that worked
I have two project, one which generates some artifacts from a WSDL file using

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.