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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:39:29+00:00 2026-06-08T12:39:29+00:00

I have the following list of variables and a mastervariable a = (1,5,7) b

  • 0

I have the following list of variables and a mastervariable

a = (1,5,7)

b = (1,3,5)

c = (2,2,2)

d = (5,2,8)

e = (5,5,8)

mastervariable = (3,2,5)

I’m trying to check if 2 elements in each variable exist in the master variable, such that the above would show B (3,5) and D (5,2) as being elements with at least 2 elements matching in the mastervariable. Also note that using sets would result in C showing up as matchign but I don’t want to count C cause only ‘one’ of the elements in C are in mastervariable (i.e. 2 only shows up once in mastervariable not twice)

I currently have the very inefficient:

if current_variable[0]==mastervariable[0]:
            if current_variable[1] = mastervariable[1]:
                        True                        
            elif current_variable[2] = mastervariable[1]:
                        True       

  #### I don't use OR here because I need to know which variables match.

elif current_variable[1] == mastervariable[0]:   ##<-- I'm now checking 2nd element
            etc. etc.

I then continue to iterate like the above by checking each one at a time which is extremely inefficient. I did the above because using a FOR statement resulted in me checking the first element twice which was incorrect:

For i in a:
      for j in a:
           ### this checked if 1 was in the master variable and not 1,5 or 1,7

Is there a way to use 2 FOR statement that allows me to check 2 elements in a list at once while skipping any element that has been used already? Alternatively, can you suggest an efficient way to do what I’m trying?

Edit: Mastervariable can have duplicates in it.

  • 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-08T12:39:30+00:00Added an answer on June 8, 2026 at 12:39 pm

    For the case where matching elements can be duplicated so that set breaks, use Counter as a multiset – the duplicates between a and master are found by:

    count_a = Counter(a)
    count_master = Counter(master)
    count_both = count_a + count_master
    dups = Counter({e : min((count_a[e], count_master[e])) for e in count_a if count_both[e] > count_a[e]})
    

    The logic is reasonably intuitive: if there’s more of an item in the combined count of a and master, then it is duplicated, and the multiplicity is however many of that item are in whichever of a and master has less of them.

    It gives a Counter of all the duplicates, where the count is their multiplicity. If you want it back as a tuple, you can do tuple(dups.elements()):

    >>> a
    (2, 2, 2)
    >>> master
    (1, 2, 2)
    >>> dups = Counter({e : min((count_a[e], count_master[e])) for e in count_a if count_both[e] > count_a[e]})
    >>> tuple(dups.elements())
    (2, 2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of elements around 1000. Each element (objects that i read
So I have something like the following in Java: private List<SomeType>variable; // ....variable is
I have the following list of time zones that a user can choose from,
I have the following list of tuples: items = [ ('john jones', ['Director', 'Screenwriter',
I have the following List<int> collection and I need to find the highest integer
I have the following list <ul> <li id=item1>Item 1</li> <li id=item2>Item 2</li> <li>Item 3</li>
I have the following list in Python: [[1, 2], [3, 4], [4, 6], [2,
I have the following list: <s:List id=list x=4 y=4 width=99% height=99% dataProvider={recordingsShown} itemRenderer=components.VideoItemRenderer useVirtualLayout=false
Suppose I have the following list: ID3_tag = ['Title','Artist','Album','Track'] And I have a file
If we have the following list: list = ['UMM', 'Uma', ['Ulaster','Ulter']] If I need

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.