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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:46:31+00:00 2026-05-22T19:46:31+00:00

For a certain piece of code, I needed to find a way to recognise

  • 0

For a certain piece of code, I needed to find a way to recognise certain aliases. Thing is, it is not known beforehand what those aliases are.

These are my requirements:

  • If A and B are aliases, and B and C are aliases, A and C should be aliases as well.
  • Two sets of aliases should be merged when they are connected in any way.
  • In each set of aliases, one should be the primary alias.

I use the following solution, making use of what boils down to a dictionary of sets:

class Alias(object):
    def __init__(self, initial):
        self._set = {initial}
        self.initial = initial
    def add(self, alias):
        self._set.add(alias)
    def merge(self, other):
        self._set.update(other._set)
    def __iter__(self):
        return iter(self._set)

class AliasDict(object):
    def __init__(self):
        self._dict = {}
    def add(self, one, other):
        if one in self._dict:
            if other in self._dict: #merge!
                self._dict[one].merge(self._dict[other])
                for k in self._dict[other]:
                    self._dict[k] = self._dict[one]
            else:
                self._dict[one].add(other)
        elif other in self._dict:
            self._dict[other].add(one)
        else:
            self._dict[one] = self._dict[other] = Alias(one)
            self._dict[one].add(other)
    def get(self, n):
        return self._dict.get(n)
    def __contains__(self, s):
        return s in self._dict

Could this be improved upon? For example, by making use of a class in the standard library (I’ve searched, but I might have missed something useful.)

  • 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-22T19:46:31+00:00Added an answer on May 22, 2026 at 7:46 pm

    This is something you can map on a graph so I’d do:

    from networkx import Graph
    from networkx.algorithms.components.connected import connected_components
    
    # see aliases as the edges between nodes in a graph
    aliases = [('A', 'B'), ('B', 'C'), ('D','E')]
    
    g = Graph( aliases )
    
    # connected components are alias groups
    print connected_components(g) # [['A', 'C', 'B'], ['E', 'D']]
    

    You don’t specify which alias should be the primary one, so you might as well pick the first one from these lists.

    networkx module

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

Sidebar

Related Questions

I want to test the behavior of a certain piece of .NET code in
I am having a piece of code in which i delegate certain properties of
I'm having some problems with a certain piece of code which sits in my
I made a certain piece of functionality which works perfectly on Chrome, but not
Suppose I want to measure the time that a certain piece of code takes.
A certain piece of code works like this: Read a file from the disk
The following piece of C# code does not compile: public class A { public
My compiler (VC++ 6.0 sp6) has apparently gone insane. In certain pieces of code
I'm making a LaTeX document for someone. A certain piece of text looks bold
Not certain if this will get much response due to the newness of Windows

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.