<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div style="" class="markdown-here-wrapper"
data-md-url="Thunderbird">
<p style="margin: 0px 0px 1.2em ! important;">Ciao lista,</p>
<p style="margin: 0px 0px 1.2em ! important;">mi trovo a dover
lavorare con dei file .xyz che sono dei file di testo del genere</p>
<pre style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;font-size: 1em; line-height: 1.2em;margin: 1.2em 0px;"><code style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;white-space: pre; overflow: auto; border-radius: 3px; border: 1px solid rgb(204, 204, 204); padding: 0.5em 0.7em; display: block ! important;">
C -3.95384 -0.70179 0.00000
C -3.93661 -2.09651 0.00000
C -2.72014 -2.77895 -0.00000
C -1.52089 -2.06666 0.00000
C -1.53812 -0.67195 0.00000
C -2.75460 0.01049 -0.00000
H -4.90162 -0.17009 -0.00000
H -4.87097 -2.65146 0.00000
H -2.70671 -3.86559 0.00000
H -0.57311 -2.59836 0.00000
H -0.60377 -0.11700 -0.00000
H -2.76802 1.09714 -0.00000
</code></pre>
<p style="margin: 0px 0px 1.2em ! important;">ovvero ogni riga
contiene un atomo con informazione sull’elemento atomico e le
coordinate. Io ho creato una semplice namedtuple per contenere
le informazioni sull’atomo</p>
<pre style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;font-size: 1em; line-height: 1.2em;margin: 1.2em 0px;"><code style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;white-space: pre; overflow: auto; border-radius: 3px; border: 1px solid rgb(204, 204, 204); padding: 0.5em 0.7em; display: block ! important;">
class Atom(namedtuple('Atom', ['element', 'coords'])):
pass
atoms = [Atom('C', numpy.array([-3.95384, -0.70179, 0.00000])), ... ]
</code></pre>
<p style="margin: 0px 0px 1.2em ! important;">Facendo il parsing
del file di testo posso dunque generare una lista che contiene
istanze di Atom. Con una lista posso facilmente iterare su
quello che chiamerò l’indice “globale”. Il problema su cui
chiedo consigli è che contemporaneamente mi serve poter
selezionare un atomo in base ad un indice “locale”, ovvero un
indice che appartiene all’elemento chimico. In questo caso,
dovrei poter iterare sul carbonio 6 volte (indice da 0 a 5) e
sull’idrogeno pure.</p>
<p style="margin: 0px 0px 1.2em ! important;">Per esempio, vorrei
poter fare qualcosa tipo <code style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;">atoms.byElementIndex('H', 0)</code>
per selezionare il primo idrogeno.</p>
<p style="margin: 0px 0px 1.2em ! important;">Che vi pare se
faccio una cosa del genere</p>
<pre style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;font-size: 1em; line-height: 1.2em;margin: 1.2em 0px;"><code style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;white-space: pre; overflow: auto; border-radius: 3px; border: 1px solid rgb(204, 204, 204); padding: 0.5em 0.7em; display: block ! important;">
class Atoms(list):
def __init__(self):
super(Atoms, self).__init__()
self._atoms_by_elem = {}
def append(self, atom):
super(Atoms, self).append(atom)
if atom.element not in self._atoms_by_elem.keys():
self._atoms_by_elem[atom.element] = []
self._atoms_by_elem[atom.element].append(self.index(atom))
def byElementIndex(self, element, index):
global_index = self._atoms_by_elem[element][index]
return self[global_index]
atoms = Atoms()
atoms.append(Atom('C', [0, 0, 0]))
atoms.append(Atom('C', [0, 0, 1]))
atoms.append(Atom('H', [1, 1, 1]))
atoms.append(Atom('C', [0, 1, 1]))
atoms.append(Atom('H', [2, 1, 1]))
</code></pre>
<pre style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;font-size: 1em; line-height: 1.2em;margin: 1.2em 0px;"><code style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;white-space: pre; overflow: auto; border-radius: 3px; border: 1px solid rgb(204, 204, 204); padding: 0.5em 0.7em; display: block ! important;">
>>> atoms.byElementIndex('H', 0)
Atom(element='H', coords=[1, 1, 1])
</code></pre>
<p style="margin: 0px 0px 1.2em ! important;">Ovviamente ora come
ora il dizionario e la lista non rimangono sincronizzati se
rimuovo elementi da <code style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;">atoms</code>
ma per ora non mi interessa.</p>
<p style="margin: 0px 0px 1.2em ! important;">Ogni consiglio è ben
accetto.</p>
<p style="margin: 0px 0px 1.2em ! important;">Ciao,</p>
<p style="margin: 0px 0px 1.2em ! important;">Davide</p>
<div
title="MDH:PHA+Q2lhbyBsaXN0YSw8L3A+PHA+bWkgdHJvdm8gYSBkb3ZlciBsYXZvcmFyZSBjb24gZGVpIGZpbGUgLnh5eiBjaGUgc29ubyBkZWkgZmlsZSBkaSB0ZXN0byBkZWwgZ2VuZXJlPC9wPjxwPmBgYDwv
cD48cD5DJm5ic3A7Jm5ic3A7Jm5ic3A7Jm5ic3A7Jm5ic3A7Jm5ic3A7Jm5ic3A7Jm5ic3A7IC0z
Ljk1Mzg0Jm5ic3A7Jm5ic3A7Jm5ic3A7Jm5ic3A7Jm5ic3A7Jm5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xwPiZu
YnNwOyZuYnNwOyZuYnNwOyA8YnI+PC9wPjxwPjxicj48L3A+"
style="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0;"></div>
</div>
</body>
</html>