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

  • Home
  • SEARCH
  • 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 645915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:33:18+00:00 2026-05-13T21:33:18+00:00

Sorry for the double post, I will update this question if I can’t get

  • 0

Sorry for the double post, I will update this question if I can’t get things to work 🙂

I am trying to compare two files. I will list the two file content:

 File 1                           File 2

"d.complex.1"                     "d.complex.1"

  1                                 4
  5                                 5
  48                                47
  65                                21

d.complex.10                    d.complex.10

  46                                6
  21                                46
 109                               121
 192                               192

TI am trying to compare the contents of the two file but not in a trivial way. I will explain what I want with an example. If you observe the file content I have typed above, the d.complex.1 of file_1 has “5” similar to d.complex.1 in file_2; the same d.complex.1 in file_1 has nothing similar to d.complex.10 in file_2. What I am trying to do is just to print out those d.complex. which has nothing in similar with the other d.complex. Consider the d.complex. as a heading if you want. But all I am trying is compare the numbers below each d.complex. and if nothing matches, I want that particular d.complex. from both files to be printed. If even one number is present in both d.complex. of both files, I want it to be rejected.

My Code:
The method I chose to achieve this was to use sets and then do a difference. Code I wrote was:

first_complex=open( "file1.txt", "r" )
first_complex_lines=first_complex.readlines()
first_complex_lines=map( string.strip, first_complex_lines )
first_complex.close()

second_complex=open( "file2.txt", "r" )
second_complex_lines=second_complex.readlines()
second_complex_lines=map( string.strip, second_complex_lines )
second_complex.close()


list_1=[]
list_2=[]

res_1=[]
for line in first_complex_lines:
    if line.startswith( "d.complex" ):
        res_1.append( [] )
    res_1[-1].append( line )

res_2=[]
for line in second_complex_lines:
    if line.startswith( "d.complex" ):
        res_2.append( [] )
    res_2[-1].append( line )
h=len( res_1 )
k=len( res_2 )
for i in res_1:
   for j in res_2:
       print i[0]
       print j[0]
       target_set=set ( i )
       target_set_1=set( j )
       for s in target_set:
           if s not in target_set_1:
               if s[0] != "d":
                   print s

The above code is giving an output like this (just an example):
d.complex.1.dssp
d.complex.1.dssp
1
48
65

d.complex.1.dssp
d.complex.10.dssp    
46
21

109

What I would like to have is:

d.complex.1
d.complex.1 (name from file2)

d.complex.1
d.complex.10 (name from file2)

I am sorry for confusing you guys, but this is all that is required.

I am so new to python so my concept above might be flawed. Also I have never used sets before :(. Can someone give me a hand here?

  • 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-13T21:33:18+00:00Added an answer on May 13, 2026 at 9:33 pm

    You need to use difference instead of intersection, since the latter will give you items that are in both sets. You can also use the set1 – set2 syntax. See the python docs for sets.

    I think you’re after this (thanks to Rick for the original code):

    def complex_file_to_dict(filename):
        out = dict()
        for line in open(filename):
            line = line.strip()
            if line.startswith('d.complex'):
                name = line
                out[name] = set()
            elif line:
                out[name].add(line)
    
        return out
    
    res_1 = complex_file_to_dict('file1.txt')
    res_2 = complex_file_to_dict('file2.txt')
    
    for k1, set_1 in res_1.iteritems():
        for k2, set_2 in res_2.iteritems():
          print k1
          print k2
          for v in set_1 - set_2:
            print v
          print
    

    EDIT:
    You can change the loop to this:

    for k1, set_1 in res_1.iteritems():
        for k2, set_2 in res_2.iteritems():
          print k1
          print k2,
          l = [v for v in set_1 - set_2]
          print '(' + ', '.join(l) + ')'
    

    to get the output like this:

    d.complex.1
    d.complex.1 (1, 65, 48)
    d.complex.1
    d.complex.10 (1, 65, 48)
    d.complex.10
    d.complex.1 (46, 109, 192)
    d.complex.10
    d.complex.10 (109, 21)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.