Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环是硬的[Python]_Python - Fatal编程技术网

循环是硬的[Python]

循环是硬的[Python],python,Python,不是程序员的职业,请原谅,如果这是显而易见的。我不能循环:/ 我有3个清单: gene_concepts[0] = ['+0|+77|CFTR', '+12|+77|CYP2C19'] genes = ['CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2

不是程序员的职业,请原谅,如果这是显而易见的。我不能循环:/

我有3个清单:

gene_concepts[0] = ['+0|+77|CFTR', '+12|+77|CYP2C19']

genes = ['CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 
'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CYP2C19', 'CYP2C19', 
'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2C19']

haplotypes = ['CFTR F508del(CTT)', 'CFTR F508del(TCT)', 'CFTR G1244E', 
'CFTR G1349D', 'CFTR G178R', 'CFTR G551D', 'CFTR G551S', 'CFTR S1251N', 
'CFTR S1255P', 'CFTR S549N', 'CFTR S549R(A>C)', 'CFTR S549R(T>G)', 
'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 
*10', 'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10']
请注意,单倍型和基因匹配(即,单倍型列表中字符串的第一项为“CFTR”,并与基因列表中列表的第一个元素匹配…因此它们是有序的)

我想建立一个新的列表或只是输出一组字符串,这样具有相同基因的单倍型(因此基因可能相互匹配,或者单倍型字符串的第一部分的子字符串,以两者中的哪个为准)被分配一个特定的代码,该代码在gene_概念列表中找到,并对应于“|”之前的第一个术语字符串列表中的分隔符

所需输出为:

+21|+0|CFTR F508del(CTT)
+22|+0|CFTR F508del(TCT)
+23|+0|CFTR G1244E
+24|+0|CFTR G1349D
+25|+0|CFTR G178R
+26|+0|CFTR G551D
+27|+0|CFTR G551S
+28|+0|CFTR S1251N
+29|+0|CFTR S1255P
+30|+0|CFTR S549N
+31|+0|CFTR S549R(A>C)
+32|+0|CFTR S549R(T>G)
+33|+12|CYP2C19 *10
+34|+12|CYP2C19 *10
+35|+12|CYP2C19 *10
+36|+12|CYP2C19 *10
+37|+12|CYP2C19 *10
+38|+12|CYP2C19 *10
+39|+12|CYP2C19 *10
+40|+12|CYP2C19 *10
所以上面文本的第一部分是“+21…+39是临时代码”…这只是我分配给它的一个任意id来跟踪。分隔符之间的部分是我试图分配匹配基因的代码。第二个分隔符后的最后一部分是单倍型

这是我到目前为止的代码

def generate_haplotype_concepts(gene_concepts[0], haplotypes):
    temp_code_2 = 20
    index = 0

    for batch_line in gene_concepts[0]:
        gene_parent_code = batch_line.split("|")[0]
        gene_parent_medcodes.append(gene_parent_code)

    index_gene = 0
    index_parent_code = 0
    for gene in genes:
        if (index_gene == 0):
            print("+" + str(temp_code_2) + "|"
                  + gene_parent_medcodes[index_parent_code] + "|"
                  + haplotypes[index_gene])
            index_gene += 1
        elif (genes[index_gene] == genes[index_gene-1]):             
            print("+" + str(temp_code_2) + "|"
                  + gene_parent_medcodes[index_parent_code] + "|"
                  + haplotypes[index_gene-1])
        else:
            index_parent_code += 1
            print("+" + str(temp_code_2) + "|"
                  + gene_parent_medcodes[index_parent_code] + "|"
                  + haplotypes[index_gene])
        index_gene += 1
        temp_code_2 += 1  

generate_haplotype_concepts(gene_concepts[0], haplotypes) 
我的输出是:

+21|+0|CFTR F508del(CTT)
+22|+0|CFTR F508del(TCT)
+23|+0|CFTR G1244E
+24|+0|CFTR G1349D
+25|+0|CFTR G178R
+26|+0|CFTR G551D
+27|+0|CFTR G551S
+28|+0|CFTR S1251N
+29|+0|CFTR S1255P
+30|+0|CFTR S549N
+31|+0|CFTR S549R(A>C)
+32|+12|CYP2C19 *10
+33|+12|CYP2C19 *10
+34|+12|CYP2C19 *10
+35|+12|CYP2C19 *10
+36|+12|CYP2C19 *10
+37|+12|CYP2C19 *10
+38|+12|CYP2C19 *10
+39|+12|CYP2C19 *10
我看到两个问题…我缺少最后一个CFTR单倍型(+32 |+0 | CFTR S549R(T>G)应该在那里),并且我得到了一个“列表索引超出范围”错误

-----------------------------------------------------------------------
----
索引器回溯(最近的调用)
最后)
在()
55
56
--->57生成单倍型概念(基因概念[0],单倍型)
在里面
生成单体型概念(临时编码单体型)
30#+“\n”)
31指数_基因+=1
--->32 elif(基因[指数基因]==基因[指数基因-1]):
33打印(“+”+str(临时代码)+“|”
34+基因\父\子代码[索引\父\子代码]+
"|"
索引器:列表索引超出范围

为我所犯的任何错误道歉…我尝试过发布比我实际所做的更简单的代码,但问题是一样的…感谢任何帮助!

为了帮助你解决你在循环中似乎面临的主要问题,我们应该看看你在循环什么。与其循环基因,不如循环你的单倍型,然后在每次迭代中,选择感兴趣的部分并与基因列表进行比较。下面的例子将有助于说明这一点

for haplotype in haplotypes:
    if haplotype.split()[0] in genes:
        print(haplotype)
因此,这里发生的事情是,对于每个
单倍型
,我们将处理一个字符串。根据您提供的示例数据,我们可以在空格上拆分这个字符串,因此我们只能查看拆分的第一部分。在Python中,
拆分
实际上是获取一个字符串并将其转换为列表基d在某个“分隔符”上。在本例中是一个空格。然后,
[0]
部分将简单地获取此列表的第一个索引,这正是我们感兴趣的部分

从这里,您将看到我们在关键字中使用了
。这将简单地查找基因,看看感兴趣的字符串是否在
基因
列表中。现在您已经为您感兴趣的每个单倍型设置了条件。从那里,我相信您可以将该代码作为前缀,如下所示:

for haplotype in haplotypes:
    if haplotype.split()[0] in genes:
        print("your code {}".format(haplotype))

以下内容可能会有所帮助(请注意检查意外情况的重要性):

单倍型=['CFTR F508del(CTT)''CFTR F508del(TCT)''CFTR G1244E''CFTR G1349D'、'CFTR G178R'、'CFTR G551D'、'CFTR S1251N'、'CFTR S1255P'、'CFTR S549N'、'CFTR S549R(A>C)'、'CYP2C19*10'、'CYP2C19*10'、'CYP2C19*10'、'CYP19*10'、'CYP19*10']
gene#u concepts={'CFTR':0,'CYP2C19':12}字典很有用
对于单倍型中的x:
prefix=x.split()[0]#通过在空格上拆分并查看第一个空格前的子字符串来获取前缀
如果前缀在基因概念中:#我们认识这个基因概念吗?
打印(“{0}{1}”。格式(基因概念[前缀],x))
否则:#如果没有,请通知用户
print('Gene with unknown concept:“{0}”。格式(x))
给出输出:

0|CFTR F508del(CTT)
0|CFTR F508del(TCT)
0|CFTR G1244E
0|CFTR G1349D
0|CFTR G178R
0|CFTR G551D
0|CFTR G551S
0|CFTR S1251N
0|CFTR S1255P
0|CFTR S549N
0|CFTR S549R(A>C)
0|CFTR S549R(T>G)
12|CYP2C19 *10
12|CYP2C19 *10
12|CYP2C19 *10
12|CYP2C19 *10
12|CYP2C19 *10
12|CYP2C19 *10
12|CYP2C19 *10
12|CYP2C19 *10

这可能不是你想要的,但我认为更接近。通过更改字典中的值,你应该能够实现你想要的。

我认为你想要这样的东西:

gene_concepts = {} # just initializes an empty dictionary to fill on the next line

gene_concepts[0] = ['+0|+77|CFTR', '+12|+77|CYP2C19']

# we don't actually end up using the genes list, since we can get the same info from the first part of each haplotype
genes = ['CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 
'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CYP2C19', 'CYP2C19', 
'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2C19'] 

haplotypes = ['CFTR F508del(CTT)', 'CFTR F508del(TCT)', 'CFTR G1244E', 
'CFTR G1349D', 'CFTR G178R', 'CFTR G551D', 'CFTR G551S', 'CFTR S1251N', 
'CFTR S1255P', 'CFTR S549N', 'CFTR S549R(A>C)', 'CFTR S549R(T>G)', 
'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10',
'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10']

# split the gene_concepts strings into usable pieces
gene_concept_codes = {} # create a dictionary for looking up gene_concepts by gene name
for gene_concept in gene_concepts[0]:
    pieces = gene_concept.split('|')  # this turns a string like "+0|+77|CFTR" into a list like ["+0", "+77", "CFTR"]
    gene_concept_codes[pieces[2]] = pieces # add the list to the dictionary, with the gene name as key

temp_id = 20 # arbitrary to match your values, change it however you need

# for each haplotype, match it up with the right gene_concept info
for haplotype in haplotypes:
    temp_id += 1
    gene = haplotype.split()[0] # pull out the name of the gene
    print("+{}|{}|{}".format(temp_id, gene_concept_codes[gene][0], haplotype)) # gene_concept_codes[gene] will be the list like ["+0", "+77", "CFTR"], so [0] gives us the first element of that list
这将提供所需的输出:

+21|+0|CFTR F508del(CTT)
+22|+0|CFTR F508del(TCT)
+23|+0|CFTR G1244E
+24|+0|CFTR G1349D
+25|+0|CFTR G178R
+26|+0|CFTR G551D
+27|+0|CFTR G551S
+28|+0|CFTR S1251N
+29|+0|CFTR S1255P
+30|+0|CFTR S549N
+31|+0|CFTR S549R(A>C)
+32|+0|CFTR S549R(T>G)
+33|+12|CYP2C19 *10
+34|+12|CYP2C19 *10
+35|+12|CYP2C19 *10
+36|+12|CYP2C19 *10
+37|+12|CYP2C19 *10
+38|+12|CYP2C19 *10
+39|+12|CYP2C19 *10
+40|+12|CYP2C19 *10

我不确定你是否能控制它,但是你的
gene\u概念应该是一本字典。如果你转换(如我下面所做的),那么匹配操作就变成了一个简单的查找:

gene_concepts = [None]
gene_concepts[0] = ['+0|+77|CFTR', '+12|+77|CYP2C19']

genes = ['CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR',
'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CYP2C19', 'CYP2C19',
'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2C19']

haplotypes = ['CFTR F508del(CTT)', 'CFTR F508del(TCT)', 'CFTR G1244E',
'CFTR G1349D', 'CFTR G178R', 'CFTR G551D', 'CFTR G551S', 'CFTR S1251N',
'CFTR S1255P', 'CFTR S549N', 'CFTR S549R(A>C)', 'CFTR S549R(T>G)',
'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10',
'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10']

def generate_haplotype_concepts(gene_concepts, genes, haplotypes):
    """Match gene id's in (genes+haplotypes) with genes in concepts list."""

    # Convert concepts into dictionary:
    gc = { t3[2]:t3[0] for t3 in map(lambda s: s.split('|'), gene_concepts)}

    # Look up gene/haplotype prefix in gc dictionary for concept
    for gene, hap in zip(genes, haplotypes):
        concept = gc.get(gene)

        if concept is None:
            hprefix = hap.split()[0]
            concept = gc.get(hprefix)

            if concept is None:
                raise ValueError("Missing gene/prefix: {}/{} in hap {}".format(
                    gene, hprefix, hap))

        yield concept, hap

print("##### Concept|Haplotype, no ID #####")
for concept, haplotype in generate_haplotype_concepts(gene_concepts[0], genes, haplotypes):
    print("{}|{}".format(concept, haplotype))

print("\n\n##### ID|Concept|Haplotype #####")

for iden, (cept, hapl) in enumerate(generate_haplotype_concepts(gene_concepts[0], genes, haplotypes), start=21):
    print("+{}|{}|{}".format(iden, cept, hapl))

在循环的第一次迭代中,您将执行两次
index\u gene+=1
。删除
if
语句中的
index\u gene+=1
行。不要尝试手动进行分组。改用
itertools.groupby
并建立一个值列表,然后可以使用
str.join
添加管道字符例如,对于
+21 |+0 | CFTR F508del(CTT)
建立一个列表x=
['+21','+0',CFTR F508del(CTT)]
,然后执行
'.\code>。加入(x)
,它会更干净。谢谢!现在我已经将第一个单倍型重复了两次,所以我正在调试它……你没有定义一个基因父代。你不能使用[0]作为变量名的一部分。其中一个“if else”案例使用index_gene-1,其余所有案例都使用index_gene。与其(错误地)保留计数器,不如使用enumerate。Validate len(genes)==len(单倍型)或抛出异常。谢谢,我输入了一个拼写错误,gene_parent_medcodes在原始代码的上方某处被声明为空列表。还有[0]变量名中只有一个元组,由上面没有显示的函数返回,所以我访问元组中的第一个元素,它是一个列表。我不确定“if-else”有什么问题使用索引_gene-1,bc,我确实希望它在该实例中分配前面的代码。基因和单倍型的长度是相同的,并且在我的测试环境下不能不同,但我意识到
gene_concepts = [None]
gene_concepts[0] = ['+0|+77|CFTR', '+12|+77|CYP2C19']

genes = ['CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR',
'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CFTR', 'CYP2C19', 'CYP2C19',
'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2C19', 'CYP2C19']

haplotypes = ['CFTR F508del(CTT)', 'CFTR F508del(TCT)', 'CFTR G1244E',
'CFTR G1349D', 'CFTR G178R', 'CFTR G551D', 'CFTR G551S', 'CFTR S1251N',
'CFTR S1255P', 'CFTR S549N', 'CFTR S549R(A>C)', 'CFTR S549R(T>G)',
'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10',
'CYP2C19 *10', 'CYP2C19 *10', 'CYP2C19 *10']

def generate_haplotype_concepts(gene_concepts, genes, haplotypes):
    """Match gene id's in (genes+haplotypes) with genes in concepts list."""

    # Convert concepts into dictionary:
    gc = { t3[2]:t3[0] for t3 in map(lambda s: s.split('|'), gene_concepts)}

    # Look up gene/haplotype prefix in gc dictionary for concept
    for gene, hap in zip(genes, haplotypes):
        concept = gc.get(gene)

        if concept is None:
            hprefix = hap.split()[0]
            concept = gc.get(hprefix)

            if concept is None:
                raise ValueError("Missing gene/prefix: {}/{} in hap {}".format(
                    gene, hprefix, hap))

        yield concept, hap

print("##### Concept|Haplotype, no ID #####")
for concept, haplotype in generate_haplotype_concepts(gene_concepts[0], genes, haplotypes):
    print("{}|{}".format(concept, haplotype))

print("\n\n##### ID|Concept|Haplotype #####")

for iden, (cept, hapl) in enumerate(generate_haplotype_concepts(gene_concepts[0], genes, haplotypes), start=21):
    print("+{}|{}|{}".format(iden, cept, hapl))