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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:54:38+00:00 2026-06-17T13:54:38+00:00

I have a file containing space separated numbers in each line. Each line corresponds

  • 0

I have a file containing space separated numbers in each line. Each line corresponds to a list of numbers.
Now there are around 300,000 such lines (each line containing around 100 numbers on an average).
I want to find mutual intersection of all such lists i.e. first list intersected with all other lists, then second list intersected with all other lists and so on.
I am using

set(a) & set(b)  

where a and b are lists I get iterating in a double loop.
But this is taking too much time. For ex : for first list intersected with all other lists, it took about 3 minutes.
How can I do this efficiently? (may be with some other language/tool)

  • 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-17T13:54:39+00:00Added an answer on June 17, 2026 at 1:54 pm

    You should use generator expressions here, they do lazy evaluation and save a lot of memory:

    In [46]: from itertools import imap
    
    In [47]: a = [[1,2,3], [2,3,4], [3,4,5]]
    
    In [48]: reduce(set.intersection,imap(set,a))
    Out[48]: set([3])
    

    considering your file looks like this:

    1 2 3
    2 3 4
    3 4 5
    

    code:
    Using itertools.combinations():

    with open("abc.txt") as f:
        lines=(map(int,x.split()) for x in f)
        for x in combinations(lines,2):
            print x,'-->',reduce(set.intersection,imap(set,x))
       ....:         
    ([1, 2, 3], [2, 3, 4]) --> set([2, 3])
    ([1, 2, 3], [3, 4, 5]) --> set([3])
    ([2, 3, 4], [3, 4, 5]) --> set([3, 4])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a txt file containing on each line 2 values separated by a
I have imported a file containing 8 columns, each column separated by '\t' ,
I have a file, in which each line contains several words that are separated
I have a file containing a list of filenames: esocket.c esocket.h dockwin.cpp dockwin.h makefile
I have a file containing the following content 1000 line in the following format:
I want to read a text file containing space separated values. Values are integers.
I have a string containing many words with at least one space between each
I have a list of elements around 1000. Each element (objects that i read
I have a text file containing some records. Each record is splitted in 4
I have a file containing a String, then a space and then a number

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.