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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:55:52+00:00 2026-06-05T12:55:52+00:00

i know this question might look like a duplicate. but i had a hard

  • 0

i know this question might look like a duplicate. but i had a hard time trying to solve this and i couldn’t find a helpful solution for my case

i’am implementing a genetic algorithm using python for the traveling salesman problem

assume we have those lists ( tours)

a = [1,0,2,5,4,3,1]
b = [1,2,5,4,3,0,1]
c = [1,3,5,4,2,0,1]

as you can see, the [5,4] is repeated in the whole 3 lists
and a regular intersection would return all the elements in the list.

i want some function like intersect_list(a,b)

that returns [5,4]

is there a python built-in way to find this? or do you have any suggestion?.

Note : i know i can loop it to solve this , but please put in mind that in my case i have around 400 lists. and at the length of 401 each.

in other words : i want to see the common path between those lists.

please let me know if anything was unclear
thanks in advance.

  • 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-05T12:55:53+00:00Added an answer on June 5, 2026 at 12:55 pm

    After taking a look at the links posted by @pyfunc, I came up with the following:

    def shortest_of(lists):
        return min(lists, key=len)
    
    def contains_sublist(lst, sublst):
        n = len(sublst)
        return any((sublst == lst[i:i+n]) for i in xrange(len(lst)-n+1)) 
    
    def longest_common(lists):
        if not lists:
            return ()
        res = set()    
        base = shortest_of(lists)
        length = len(base)
    
        for i in xrange(length, 0, -1):
            for j in xrange(length - i + 1):
                candidate = ', ' + str(base[j:i+j]).strip('[]') + ','
                #candidate = base[j:i+j]  
    
                for alist in lists:
                    if not candidate in ', ' + str(alist).strip('[]') + ',':
                    #if not contains_sublist(alist, candidate):   
                        break
                else:
                    res.add(tuple([int(a) for a in candidate[2:-1].split(',')]))
                    #res.add(tuple(candidate))
    
            if res:
                return tuple(res)    
    
        return ()
    
    if __name__ == '__main__':
        a = [1,0,2,5,4,3,1]
        b = [1,2,5,4,3,0,1]
        c = [1,3,5,4,2,0,1]
    
        print longest_common([a,b,c])
        print longest_common([b,c])
    

    output:

    ((5, 4),)
    ((0, 1), (5, 4))
    

    EDIT:

    Updated solution to use string conversions and matching as it happened to be way faster. Previous solution parts are commented out. Also, it now gives all possibilities.

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

Sidebar

Related Questions

I know this question might have already been discussed a thousands of times but
I know this might seem a controversial question but it really is not meant
This might sound a bit of an odd question but I know what I
This might be a simple question, but I'll ask it anyways. I know when
I know, this might be a very basic question but I am not 100%
I know this question is overly popular, but I find nothing that addresses rewriting
I know this question might be considered better on Stack UI, but I wanted
This might sound like a bit of a silly question but I been looking
I know this question has kind-a started religious wars in past and there might
I know this question has been done but I have a slightly different twist

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.