0

I got this issue when trying to improve a clustering algorithm. I need the labels to form a chain and be assigned a value when the chain terminates.

I simpler version of the problem is explained below.

A = []
B = []
C = []

C = B
B = A
A.append(0)

This gives: A = [0], B = [0], C = []

I want something which will instantly update all the elements when an end is reached and edited. Like I want all A,B and C to be linked together and get updated when A is changed. Expected result: A = [0], B = [0], C = [0] ... (This chain can be of any length. It may even develop branches.)

How do I go about achieving this? I put the title that way because I felt it could be done if C pointed to where A points when B = A assignment is done.

Note: I cannot keep track of the head of the whole chain at a time. This linking variables part is done based on some other order.

Note: This connection forms part by part. So A = B = C = [0] is not helpful.

11
  • 3
    There's no chain here, just a bunch of references to the same thing. Are you looking for a linked list? Commented Jun 13, 2019 at 12:46
  • 1
    A = B = C = []? Commented Jun 13, 2019 at 12:46
  • Variables in Python are not assigned memory but objects and they will point to the same object until they are assigned another object. Commented Jun 13, 2019 at 12:56
  • C = B means that C and B now refer to the same list. B = A means that B and A now refer to the same list. So you now just have two lists, those originally assigned to B (now referenced by C) and A(now referenced by B). Commented Jun 13, 2019 at 12:58
  • I have updated the question to provide more clarity about the exact issue being faced. Commented Jun 13, 2019 at 13:15

3 Answers 3

1

You can do it this way:

>>> A = B = C = []
>>> A.append(0)
>>> A
[0]
>>> B
[0]
>>> C
[0]

In Python, a variable acts as a pointer to a value. When you run A = B = C = [] you are basically saying that A, B and C are pointing to the same list in memory.

Alternatively, you can do the following:

>>> A = B = []
>>> C = A
>>> C.append(0)
>>> A
[0]
>>> B
[0]
>>> C
[0]
>>> B.append(1)
>>> A
[0, 1]
>>> B
[0, 1]
>>> C
[0, 1]
Sign up to request clarification or add additional context in comments.

5 Comments

The chain forms part by part. I can't assign them like that directly.
Updated my answer.
@VenuNathan did that helped?
Thanks for answering but still not that helpful. I'll update the question to explain more in a minute.
Updated the question, would you please have a look.
0

References:

Thank you so much to Mad Physicist, cdrake and Rafael for your suggestions which steered me in the direction I needed to be in to reach the answer.

Answer:

The trick is to never make assignments such as C = B; B = A;, as this will cause loss of information when variables start referring to new objects and the old objects are abandoned and left for some variables to uselessly refer to.

Instead we must make each of these already declared objects to store information as to how to reach the end of the chain. This can be done by:

# Initial Declaration
A = []; B = []; C = []; D = []

# Chain Formation
C.append(B)
B.append(A)
D.append(C)

# Data Addition at Chain head
A.append("DATA")

This gives:

>>> A
['DATA']

>>> B
[['DATA']]

>>> C
[[['DATA']]]

>>> D
[[[['DATA']]]]

Now each of the variables has access to the data added at Chain Head. This data can be obtained by repeatedly entering the into the index 0 of the list till we reach an object which is not of type <class 'list'>. The implementation given below will make it clear:

def GetData(Chain):
    C = Chain.copy()
    while len(C) > 0 and type(C[0]) == type([]):
        C = C[0]
    if len(C):
        return C[0]
    else:
        return None

>>> ( GetData(A), GetData(B), GetData(C), GetData(D) )
('DATA', 'DATA', 'DATA', 'DATA')

This way of storing information so as to direct the preceding members to the same source of information as the head of the Chain is very helpful as it can instantly relay the change of final information to the members at the back. An example which builds over the previous code is shown below:

# Initial Declaration for New Chain
P = []; Q = []

# New Chain Formation
Q.append(P)

# Old Chain Redirection
C.remove(C[0])
C.append(Q)

# Attempted Data Recovery
>>> ( GetData(A), GetData(B), GetData(C), GetData(D), GetData(P), GetData(Q) )
('DATA', 'DATA', None, None, None, None)

# Data Addition at New Chain Head
P.append((0,0))

# Attempted Data Recovery
>>> ( GetData(A), GetData(B), GetData(C), GetData(D), GetData(P), GetData(Q) )
('DATA', 'DATA', (0, 0), (0, 0), (0, 0), (0, 0))

This is my solution to the problem I was facing. Please feel free to suggest changes if needed.

Once again, Thank you to all those who directed me to this solution.

1 Comment

See also copy.deepcopy() in the standard library. docs.python.org/3.7/library/copy.html
0

Everything in Python is object. Variables are are just names, no relation with locations.

Names refer to objects. Names are introduced by name binding operations.

Execution model: naming and binding — Python 3.7.3 documentation

Every variable in Python is just a name, which is bound to one object.

For variable in Python, nothing is constant except for the name itself.

However, variable in C is indeed a memory location.

// c
// variable a has one location
// variable b has one location, too
// they are of course different
int a, b; 

// value of location of variable a will become 4
a = 4; 

// location of variable b don't change
// what changed is 
// value of location of variable b 
// will become value of location of variable a
b = a; 
# python
# variable a(just a name) is bound to object 4
a = 4

# variable b(just a name) is bound to the object which variable a is bound
b = a

# variable b(just a name) is bound to another object 6
# nothing to with variable a(just a name)
b = 6

So:

Is there a way in python to assign a variable such that it always points to the memory where its assigned right hand side points to?

Not in Python. Variables in Python just don't point to memory directly in fact. Variables are just names.

2 Comments

True, but not relevant without much further explanation.
@MadPhysicist I have tried to explain in more detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.