You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将PyMOL中cmd.iterate的执行结果存入变量?

实现方法

要将grab函数的结果存入my_final_list,只需在命名空间中初始化列表,并修改cmd.iterate的执行语句,让每次迭代时将结果追加到列表中:

修改后的完整代码

def grab(resi):
    s1 = f"resi {resi} & name P"
    s2 = f"resi {resi} & name C4'"
    s3 = f"resi {int(resi) + 1} & name P"
    s4 = f"resi {int(resi) +1 } & name C4'"
    
    try:
        dih = round(cmd.get_dihedral(s1,s2,s3,s4),2)
    except:
        dih = None

    return s1,s2,s3,s4,dih

# 初始化包含grab函数和空列表的命名空间
myspace = {"grab": grab, "my_final_list": []}
# 迭代时将grab的返回值追加到列表中
cmd.iterate("(name P)","my_final_list.append(grab(resi))", space=myspace)

# 此时my_final_list已包含所有结果
print(myspace["my_final_list"])

关键说明

  • myspace中新增空列表my_final_list,让迭代过程能直接访问并修改这个列表
  • cmd.iterate的第二个参数改为my_final_list.append(grab(resi)),这样每次处理一个匹配原子时,都会把grab返回的元组添加到列表里
  • 执行完cmd.iterate后,myspace["my_final_list"]就会存储你需要的所有结果,格式与你期望的完全一致

内容的提问来源于stack exchange,提问作者Riccardo Fusco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 10:00:51