diff --git a/2025.02.25_python_connecting_multiple_attributes/class_approach.py b/2025.02.25_python_connecting_multiple_attributes/class_approach.py new file mode 100644 index 0000000..feeef44 --- /dev/null +++ b/2025.02.25_python_connecting_multiple_attributes/class_approach.py @@ -0,0 +1,24 @@ +""" +This code is supported by the website: https://www.guanjihuan.com +The newest version of this code is on the web page: https://www.guanjihuan.com/archives/45135 +""" + +class Atom: + def __init__(self, name='atom', index=0, x=0, y=0, z=0): + self.name = name + self.index = index + self.x = x + self.y = y + self.z = z + +atom_object_list = [] +index = 0 +for i0 in range(3): + for j0 in range(3): + atom = Atom(index=index, x=i0, y=j0) + atom_object_list.append(atom) + index += 1 + +print(atom_object_list) +for atom_object in atom_object_list: + print([atom_object.name, atom_object.index, atom_object.x, atom_object.y, atom_object.z]) \ No newline at end of file diff --git a/2025.02.25_python_connecting_multiple_attributes/dictionary_approach.py b/2025.02.25_python_connecting_multiple_attributes/dictionary_approach.py new file mode 100644 index 0000000..69b0b06 --- /dev/null +++ b/2025.02.25_python_connecting_multiple_attributes/dictionary_approach.py @@ -0,0 +1,22 @@ +""" +This code is supported by the website: https://www.guanjihuan.com +The newest version of this code is on the web page: https://www.guanjihuan.com/archives/45135 +""" + +atom_dict_list = [] +index = 0 +for i0 in range(3): + for j0 in range(3): + atom_dict= { + 'name': 'atom', + 'index': index, + 'x': i0, + 'y': j0, + 'z': 0, + } + atom_dict_list.append(atom_dict) + index += 1 + +print(atom_dict_list) +for atom_dict in atom_dict_list: + print([atom_dict['name'], atom_dict['index'], atom_dict['x'], atom_dict['y'], atom_dict['z']]) \ No newline at end of file