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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:10:15+00:00 2026-06-02T15:10:15+00:00

I have two lists, A and B, with an equal number of elements, although

  • 0

I have two lists, A and B, with an equal number of elements, although the elements in each list are not necessarily distinct.

I would like to form a new list by coupling the elements from A and B at random (the random pairing is important).

However, I also need to make sure that each pair in the resulting list is unique.

So far, I’ve been approaching the problem as follows, which works for small lists, but clearly is not suited to larger lists with many combinations.

from random import shuffle

# Create a list of actors and events for testing
events = ['P1','P1','P1','P2','P2','P2','P3','P3','P3','P4','P5','P6','P7','P7']
actors = ['IE','IE','ID','ID','IA','IA','IA','IC','IB','IF','IG','IH','IH','IA']

# Randomize the elements of each list
shuffle(events)
shuffle(actors)

# Merge the two lists into a new list of pairs
edgelist = zip(events,actors)

# If the new list of pairs has all unique elements, then it is a good solution, otherwise try again at random
x = set(edgelist)

if len(edgelist) == len(x):
  break
else:
  while True:
    shuffle(events)
    shuffle(actors)
    edgelist = zip(events,actors)
    x = set(edgelist)
    if len(edgelist) == len(x):
      break

# Display the solution
print 'Solution obtained: '
for item in edgelist:
  print item

Can anyone suggest a modification or alternative approach that would scale to larger input lists?

Thanks for the helpful replies.

Update

Turns out this is a more challenging problem than originally thought. I think I now have a solution. It may not scale incredibly well but works fine for small or medium sized lists. It checks to see whether a solution is possible before starting, so assumptions about the distribution of the input lists aren’t necessary. I also included a few lines of code to show that the frequency distributions of the resulting list match the original.

# Randomize the elements
shuffle(events)

# Make sure a solution is possible
combinations = len(set(events))*len(set(actors))
assert combinations >= len(events) and combinations >= len(actors) and len(events) == len(actors), 'No soluton possible!'

# Merge the two lists into a new list of pairs (this will contain duplicates)
edgelist = zip(events,actors)

# Search for duplicates
counts = collections.Counter(edgelist)
duplicates = [i for i in counts if counts[i] > 1]
duplicate_count = len(duplicates)

while duplicate_count > 0:

  # Get a single duplicate to address
  duplicate = duplicates[0]

  # Find the position of the duplicate in the in edgelist
  duplicate_pos = edgelist.index(duplicate)

  # Search for a replacement
  swap = choice(edgelist)
  swap_pos = edgelist.index(swap)

  if (swap[0],duplicate[1]) not in edgelist: 
    edgelist[duplicate_pos] = (swap[0],duplicate[1])
    edgelist[swap_pos] = (duplicate[0],swap[1])

  # Update duplicate count
  counts = collections.Counter(edgelist)
  duplicates = [i for i in counts if counts[i] > 1]
  duplicate_count = len(duplicates)


# Verify resulting edgelist and frequency distributions

print 'Event Frequencies: '
print sorted([y for (x,y) in list(collections.Counter(events).items())], reverse=True)

print 'Edgelist Event Frequencies: '        
print sorted([y for (x,y) in list(collections.Counter([x for (x,y) in edgelist]).items())], reverse=True)

print 'Actor Frequencies: '        
print sorted([y for (x,y) in list(collections.Counter(actors).items())], reverse=True)

print 'Edgelist Actor Frequencies: '        
print sorted([y for (x,y) in list(collections.Counter([y for (x,y) in edgelist]).items())], reverse=True)

assert len(set(edgelist)) == len(events) == len(actors)
  • 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-02T15:10:16+00:00Added an answer on June 2, 2026 at 3:10 pm

    Well, there is no reason for you to shuffle both lists. The pairing will not get “more random”.

    Update: I’ve posted a different solution than my original one. It’s recursive, and is guaranteed to always return a valid answer, or None if one is not possible.

    from random import shuffle
    
    def merge(events, actors, index=None):
        """Returns list of unique pairs of events and actors, none of which may on index"""
        if len(events) == 0:
            return []
        if index is None:
            index = set()
    
        merged = None
        tried_pairs = set()
        for event in events:
            for i, actor in enumerate(actors):
                pair = (event, actor)
                if pair not in index and pair not in tried_pairs:
                    new_index = index.union([pair])
                    rest = merge(events[1:], actors[:i]+actors[i+1:], new_index)
    
                    if rest is not None:
                        # Found! Done.
                        merged = [pair] + rest
                        break
                    else:
                        tried_pairs.add(pair)
            if merged is not None:
                break
    
        return merged
    
    
    if __name__ == '__main__':
        _events = ['P1','P1','P1','P2','P2','P2','P3','P3','P3','P4','P5','P6','P7','P7']
        _actors = ['IE','IE','ID','ID','IA','IA','IA','IC','IB','IF','IG','IH','IH','IA']
        shuffle(_events)
        edgelist = merge(_events, _actors)
    
        if edgelist is not None:
            print 'Solution obtained: '
            for item in edgelist:
                print item
        else:
            print 'No possible solution with these events and actors.'
    

    Note: the “index” variable is similar to checking edgelist, on the OP’s solution. The “tried_pairs” variable is just an optimisation for each specific recursion step, to avoid retrying the same pair over and over again (if, for instance, there are several consecutive identical items in actors).

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

Sidebar

Related Questions

In Python, I have two lists that either have equal number of elements (e.g.
We have two lists: a=['1','2','3','4'] b=['2','3','4','5'] How to get a list with elements that
I have two order lists with comparable information but list item contents may not
i have two lists: List<comparerobj> list_c = new List<comparerobj>(); List<comparerobj> list_b = new List<comparerobj>();
I have two Lists, the firs (right) represents a list of all cars and
I have two lists in a dialog - one bubble count list and one
I want to calculate , in two lists (same lenght), the number of elements
I have two lists of the same type. That type does not have an
I have two unsorted lists and I need to produce another list which is
I have two lists which are guaranteed to be the same length. I want

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.