I would like to construct a neighbour list of multiple atoms.

``````AllAtoms = {}
AllAtoms[0] = [0,[]]
AllAtoms[1] = [0,[AllAtoms[0][0]]]
AllAtoms[2] = [0,[AllAtoms[0][0]]]``````

Every member of the dictionary is referencing other members of the dictionary. I do not know how to make this reference work so that when a change is made e.g

``AllAtoms[0] = [99,[]]``

, the members referencing this object also change.

Thank you
Andy
python 2.6

## All 7 Replies

You should explain a little more. You are creating a dictionary

``number1 --->   [  number2,   list1  ]``

I suppose that number1 is an atom ID, but what are number2 and list1 ?

number2 is number1's atom type (an integer representing a chemical element)
list1 is a list of the types of number1's neighbouring atoms.

Why don't you store instead a list of the neighbouring atoms ? This would solve your problem.

Python uses references to point to a list, so you can use lists to do what you want. A linked list is one method used to do this depending on the specifics.

``````AllAtoms = {}
AllAtoms[0] = [0,[]]
AllAtoms[1] = [0,[AllAtoms[0]]]
AllAtoms[2] = [0,[AllAtoms[0]]]

print "Original"
for item in AllAtoms:
print item, AllAtoms[item]

print "-"*50
AllAtoms[0].append(9)
for item in AllAtoms:
print item, AllAtoms[item]

##-----  OR  -------------------------------------------------
print "-"*50
AllAtoms = {}
AllAtoms[0] = [[0],[1]]
AllAtoms[0][0] = [9]
AllAtoms[1] = [0,[AllAtoms[0]]]
AllAtoms[2] = [0,[AllAtoms[1]]]
for item in AllAtoms:
print item, AllAtoms[item]
#
#-----  My Output
0 [0, []]
1 [0, [[0, []]]]
2 [0, [[0, []]]]
--------------------------------------------------
0 [0, [], 9]
1 [0, [[0, [], 9]]]
2 [0, [[0, [], 9]]]
--------------------------------------------------
0 [[9], [1]]
1 [0, [[[9], [1]]]]
2 [0, [[0, [[[9], [1]]]]]]``````

Thanks for the responses,

The second option of woooee is almost suitable, but it would be very difficult to access the information I want in those linked lists. Is it possible to link *only* the first one or two values from a linked list?
The problem with

``````AllAtoms = {}
AllAtoms[0] = [[0],[1]]
AllAtoms[0][0] = [9]
AllAtoms[1] = [0,[AllAtoms[0]]]
AllAtoms[2] = [0,[AllAtoms[1]]]
for item in AllAtoms:
print item, AllAtoms[item]
#with output
0 [[9], [1]]
1 [0, [[[9], [1]]]]
2 [0, [[0, [[[9], [1]]]]]]``````

is that the values I want get more and more deeply nested, and since each list member is storing the entirety of the other members (including their neighbour lists), the list gets too large. I would like to write something like this that stores only the value I want (i.e. AllAtoms[0]), but produces the output below:

``````AllAtoms = {}
AllAtoms[0] = [[0],[1]]
AllAtoms[1] = [0, AllAtoms[0][0]]
print AllAtoms[1][1][0]
AllAtoms[0][0] = [9]
print AllAtoms[1][1][0]
AllAtoms[0][0] = [6]
print AllAtoms[1][1][0]``````

that would output

``````0
9
6``````

Any ideas how? Thanks for any assistance
Andy

If you want a practical access to the data, the best way is to use a class

``````class Atom(object):
def __init__(self, num, elt, neigh=()):
self.num = num
self.elt = elt
self.neigh = set(neigh)

def __str__(self):
return "Atom(%s, %s)" % (self.num, self.elt)

def __lt__(self, other):
return self.num < other.num

def neigh_elts(self):
for atom in sorted(self.neigh):
yield atom.elt

def display_atoms(dic):
for num, atom in sorted(dic.items()):
print atom, list(atom.neigh_elts())

AllAtoms = {}
AllAtoms[0] = Atom(0, 0)
AllAtoms[1] = Atom(1, 0, [AllAtoms[0]])
AllAtoms[2] = Atom(2, 0, [AllAtoms[0], AllAtoms[1]])

display_atoms(AllAtoms)

AllAtoms[0].elt = 9

print "Atom 0's element set to 9"
display_atoms(AllAtoms)

""" My output --->
Atom(0, 0) []
Atom(1, 0) [0]
Atom(2, 0) [0, 0]
Atom 0's element set to 9
Atom(0, 9) []
Atom(1, 0) [9]
Atom(2, 0) [9, 0]

"""``````

With a class, you can customize the access to data the way you want. You could also define a class AtomCollection instead of using a dict.

The example using a class works practically and elegantly for my needs,
Thank you very much
Andy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.