【python】combinations函数遍历列表内元素不同组合

在参数调优的过程中,不同个体数的样本组合需要计算,但是一个一个用for来穷尽组合的可能显得太过笨拙,查到可以用itertools中的combinations模块来处理类似的问题:

复制代码
from itertools import combinations
from sys import argv
import os

lst=[]

with open(argv[1], "r") as fi:
    for lines in fi.readlines():
        line = lines.strip("\n")
        lst.append(line)
for j in range(9):
    comlst = list(set(combinations(lst,j)))
    for i in comlst:
        filename = "allcombine_"+str(j)+"_combine_"+str(comlst.index(i))+"_.bed"
        for k in i:
            print(k)
            os.system('cat {} >> tmp/{}'.format(k,filename))
            os.system('bedtools sort -i tmp/{} > tmp/{}.sorted'.format(filename,filename))
            os.system('bedtools merge -i tmp/{}.sorted > result/{}.sorted.merged'.format(filename,filename))
            os.system('rm tmp/{}'.format(filename))

fi.close()
复制代码

combinations输出的结果是一个{}限定起来的组合,不能直接当列表用会报错。

posted @   xjce  阅读(244)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示