在下面的脚本中
atind=[5,37,43,55,75,85,104,127,141,147,168,182,197,203,213,235,245,269,288,307,331,352,372,393,407,417,439,449,456,475,485,502,516,536,552,573,580,587,597,621,631,653,677,691,705,725,747,758,768,783,795,805,822,846,860,870,877,884]
for f in feat.describe_features(ds[0]):
[a,b]=f['resseqs']
print("d: DISTANCE ATOMS=%d,%d"%(atind[a-1], atind[b-1]))
我得到的结果是
d: DISTANCE ATOMS=85,141
d: DISTANCE ATOMS=85,147
d: DISTANCE ATOMS=85,168
d: DISTANCE ATOMS=85,182
d: DISTANCE ATOMS=85,197
d: DISTANCE ATOMS=85,203
d: DISTANCE ATOMS=85,213
……有1000多行这样的代码,我想要的是一种方法,这样我就可以编辑我的脚本来对输出进行编号,比如
d1: DISTANCE ATOMS=85,141
d2: DISTANCE ATOMS=85,147
d3: DISTANCE ATOMS=85,168
d4: DISTANCE ATOMS=85,182
d5: DISTANCE ATOMS=85,197
d6: DISTANCE ATOMS=85,203
d7: DISTANCE ATOMS=85,213
……直到最后一次。
。。我想我错过了最基本的Python方法。任何帮助都将不胜感激。
我想它应该是一个变量,在
d$i
之后,也许不是? 最佳答案
对迭代器使用enumerate
生成索引值对:
for i, f in enumerate(feat.describe_features(ds[0]), start=1):
[a,b]=f['resseqs']
print("d%d: DISTANCE ATOMS=%d,%d"%(i, atind[a-1], atind[b-1]))
如果使用Python3.6+,请注意,可以使用f字符串进行更好的格式化:
for i, f in enumerate(feat.describe_features(ds[0]), start=1):
a, b = f['resseqs']
print(f"d{i}: DISTANCE ATOMS={atind[a - 1]},{atind[b - 1]}")
关于python - 在python中为每一行编号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51001018/