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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:51:22+00:00 2026-06-13T02:51:22+00:00

first post, be as critical as you want… My problem: I have a really

  • 0

first post,
be as critical as you want…

My problem:
I have a really large file of 140 million lines (file 1) and a slightly smaller file of 3 million lines (file 2). I want to remove those lines in file 1 that have matches to file 2. Intuitively this seemed like a simple find and remove problem that shouldn’t take that long..relatively. As my code stands its taking ~4 days to run on a 24Gb processor. I’d like to perform this on several files and so I would like an improvement on time.
Any help and comments would be much appreciated.

Sample file1:

reftig_0 43 0 1.0
reftig_0 44 1 1.0
reftig_0 45 0 1.0
reftig_0 46 1 1.0
reftig_0 47 0 5.0

Sample file 2:

reftig_0 43
reftig_0 44
reftig_0 45

Code:

data = open('file_1', 'r')
data_2 = open('file_2', 'r')
new_file = open('new_file_1', 'w')

d2= {}
for line in data_2:
    line= line.rstrip()
    fields = line.split(' ')
    key = (fields[0], fields[1])
    d2[key]=1

#print d2.keys()
#print d2['reftig_1']
tocheck=d2.keys()
tocheck.sort()
#print tocheck

for sline in data:
    sline = sline.rstrip()
    fields = sline.split(' ')
    nkey = (fields[0],fields[1])
    #print nkey
    if nkey in tocheck:
        pass
    else:
        new_file.write(sline + '\n')
        #print sline
  • 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-13T02:51:23+00:00Added an answer on June 13, 2026 at 2:51 am

    Writing short strings to new_file once per line is slow. Reduce the number of writes by appending content to a list, and writing to new_file only when the list is, say, 1000 lines long.

    N = 1000
    with open('/tmp/out', 'w') as f:
        result = []
        for x in range(10**7):
            result.append('Hi\n')
            if len(result) >= N:
                f.write(''.join(result))
                result = []
    

    Here is the result of running time test.py for various values of N:

    |      N | time (sec) |
    |      1 |      5.879 |
    |     10 |      2.781 |
    |    100 |      2.417 |
    |   1000 |      2.325 |
    |  10000 |      2.299 |
    | 100000 |      2.309 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First post - hope I'm doing it right! I have a file, lexicon.plist, containing
First post, but long time browser :) So here's my problem: Basically I have
first post so be nice :) My problem is that I have a class
First post, have found many answers here, so hopes are high. The problem: Google
First post here and it's and awk question. I have a file that looks
My first post here :) I have a summer job doing a bit of
This is my first post and I my first experience with jquery. I have
First post here. Having a problem with NSNumber's floatValue method -- somehow, it returns
My first post here. I want to make a horizontal menu with submenu's sliding
First post. I've searched high and low for similar questions and have come up

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.