<p>将数据读入带有<a href="http://docs.python.org/library/csv.html#csv.reader" rel="nofollow">csv.reader</a>的元组列表后,按<code>(x, y)</code>值对数据进行排序。为了清楚起见,请使用<a href="http://docs.python.org/library/collections.html#collections.namedtuple" rel="nofollow">named tuples</a>来标识字段。在</p>
<p>然后使用<a href="http://docs.python.org/library/itertools.html#itertools.groupby" rel="nofollow">itertools.groupby</a>对相关的<code>(x, y)</code>数据点进行聚类。对于每一组,使用<a href="http://docs.python.org/library/functions.html#min" rel="nofollow">min</a>来隔离能量最低的那一组:</p>
<pre><code>>>> import csv, collections, itertools
>>> raw_data = '''\
-2.800000,-1.000000,5.470000,-0.26488315
-3.000000,1.000000,4.070000,-0.81185718
-2.800000,-1.000000,3.270000,1.29303723
-2.800000,-0.400000,4.870000,-0.51165026
'''.splitlines()
>>> Sample = collections.namedtuple('Sample', ['x', 'y', 'z', 'energy'])
>>> data = [Sample(*row) for row in csv.reader(raw_data)]
>>> data.sort(key=lambda s: (s.x, s.y))
>>> for xy, group in itertools.groupby(data, key=lambda s: (s.x, s.y)):
print min(group, key=lambda s: s.energy)
Sample(x='-2.800000', y='-0.400000', z='4.870000', energy='-0.51165026')
Sample(x='-2.800000', y='-1.000000', z='5.470000', energy='-0.26488315')
Sample(x='-3.000000', y='1.000000', z='4.070000', energy='-0.81185718')
</code></pre>