'How to access elements of a list in a class object?
I'm new to python and trying to understand the basics using the pyscal module. I am trying to find the positions of each atom in each molecule at each snapshot (timepoint). From what I understand, customkeys in the following defines a list, whereas sys is a class object containing the different snapshots of my system, where each snapshot is a timepoint containing the positions of all atoms in all molecules.
I want to do something like iterate over the class object to obtain the positions of all of the atoms in each molecules at each timepoint. So I do the following:
sys = ml.to_system(customkeys=["mol"])
for mol in sys:
each_molec = atom.pos
print(each_molec)
print(mol)
The first print statement outputs only the positions of the very last atom in the system, and the second print statement outputs something that I don't understand like:
<pyscal.core.System object at 0x7f84eab9a900>
Alternatively, I can access the molecule number for a particular atom in an particular snapshot by something like:
sys[1].atoms[1].custom["mol"]
How, then, do I properly access the list in the class object and output the positions for each element (if that's the right word) of the list?
Solution 1:[1]
The System object contains a list of atoms. So, I suspect you need something like:
system = ml.to_system(customkeys=["mol"])
for mol in system.atoms:
print("Another molecule")
for atom in mol:
print(atom.pos)
However, the pyscal module is large and complicated. It's possible I misunderstood the documentation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
