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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:53:52+00:00 2026-05-31T14:53:52+00:00

I have two sets of lists that are synchronized and look like this: (by

  • 0

I have two sets of lists that are synchronized and look like this:
(by synchronized I mean that ‘A’ in cal belongs to 12 in cpos, and ‘A’ in mal belongs to 11 in mpos)

set1

cpos = [12, 13, 14, 15]
cal = ['A', 'T', 'C', 'G']

set2

mpos = [11, 12, 13, 16]
mal = ['A', 'T', 'T', 'G']

I want to find a match between the two sets, and in this example there is only one match, 13T in cpos&cal and 13T in mpos&mal.

I wrote this script, but it only compares the values by indices it seems, since the match string is empty:

mat = []
for i in xrange(len(cpos)):
     if mpos[i] == cpos[i] and mal[i] == cal[i]:
             mat.append(cpos[i])

This is what I wanted to get:

mat = [13]

Any ideas how to solve this?

  • 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-31T14:53:54+00:00Added an answer on May 31, 2026 at 2:53 pm
    cpos = [12, 13, 14, 15]
    cal = ['A', 'T', 'C', 'G']
    
    mpos = [11, 12, 13, 16]
    mal = ['A', 'T', 'T', 'G']
    
    set1 = set(zip(cpos, cal))
    set2 = set(zip(mpos, mal))
    
    print set1 & set2
    

    Result:

    ## set([(13, 'T')])
    

    As per @Janne Karila’s comments below, the following will be more efficient:

    from itertools import izip
    print set(izip(cpos, cal)).intersection(izip(mpos, mal))
    

    Timings:

    import timeit
    
    repeat = 1
    
    setup = '''
    num = 1000000
    import random
    import string
    from itertools import izip
    cpos = [random.randint(1, 100) for x in range(num)]
    cal = [random.choice(string.letters) for x in range(num)]
    mpos = [random.randint(1, 100) for x in range(num)]
    mal = [random.choice(string.letters) for x in range(num)]
    '''
    
    # izip: 0.38 seconds (Python 2.7.2)
    t = timeit.Timer(
         setup = setup,
         stmt = '''set(izip(cpos, cal)).intersection(izip(mpos, mal))'''
    )
    
    print "%.2f second" % (t.timeit(number=repeat))
    
    
    
    # zip: 0.53 seconds (Python 2.7.2)
    t = timeit.Timer(
         setup = setup,
         stmt = '''set(zip(cpos, cal)) & set(zip(mpos, mal))'''
    )
    
    print "%.2f second" % (t.timeit(number=repeat))
    
    
    # Nested loop: 616 seconds (Python 2.7.2)
    t = timeit.Timer(
         setup = setup,
         stmt = '''
    
    mat = []
    for i in xrange(len(cpos)):
         for j in xrange(len(mpos)):
              if mpos[j] == cpos[i] and mal[j] == cal[i]:
                   mat.append(mpos[j]) # or mat.append((mpos[j], mal[j])) ?
                   break
    '''
    )
    
    print "%.2f seconds" % (t.timeit(number=repeat))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two sets of data, (Ax, Ay; Bx, By). I'd like to plot
Assume that we have two sets: A=(a_1,a_2,...,a_m) and B=(b_1,b_2,...,a_n) (Not necessarily of same size).
I have two separate sets of tables in the same database that model the
I have a program that sets an alert dialog that asks Would you like
I have two lists that i need to combine where the second list has
I have a two data sets as lists, for example: xa = [1, 2,
say I have two sets of shape files that cover the same region and
I have two sets (although I can do lists, or whatever): a = frozenset(('Today','I','am','fine'))
I have a two properties which holds lists. Whenever any item in this list
suppose you are given the following problem. You have two index sets that have

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.