使用生成器表达式的简单解决方案From PEP 289 Generator Expressions
Rationale
Experience with list comprehensions has shown their widespread utility
throughout Python. However, many of the use cases do not need to have a full list created in memory. Instead, they only need to iterate over the elements one at a time.
因为你不需要保存中间结果
可能你有一个大的数据集
以及itertools标准库模块中的^{},因为您需要计算数据集中每对有趣的点的距离。在$ cat euclid.py
from scipy.spatial.distance import euclidean
from itertools import combinations
lines = ['HETATM 1 H10 XSHQ 0 10.139 2.231 0.091 1.00 0.00 H',
'HETATM 2 N1 XSHQ 0 9.641 1.386 -0.104 1.00 0.00 N',
'HETATM 3 H9 XSHQ 0 9.773 1.133 -1.063 1.00 0.00 H',
'HETATM 4 C1 XSHQ 0 8.245 1.531 0.230 1.00 0.00 H']
H_lines = (line for line in lines if line[-1]=='H')
H_lists = (line.split() for line i