如何在Python中为循环元组函数纠正这个问题?

2024-09-28 01:31:19 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个程序,每一行用元组分成一对,这样每个字母表都有一个对应的数字,如a:6,B:6,C:35等。如果值小于10,则字母表被转换成N。下面是代码。我发现我的代码在代码的最后一部分没有循环到元组函数上。它只接受一个序列,不在另一个序列上循环

tutorial = open('c:/test/z.txt','r')
## Input file looks like
>qrst
ABCDE--  6  6 35 25 10
>qqqq
ABBDE--  7  7 28 29  2

org = []
seqlist = []
seqstring = ""
for line in tutorial:
    if line.startswith(">"):
        if seqstring!= "":
            seqlist.append(seqstring)
            seqstring = ""
        org.append(line.rstrip("\n"))
    else:
        seqstring += line.rstrip("\n")
seqlist.append(seqstring)
l = seqlist
#print l

j = []
ll = len(seqlist)
for i in range(0,ll):
    sq = l[i]
    sequence = sq.split(" ")[0] ## Stores only the alphabets
    qualities = sq.split(" ")[1:] ## Stores only the numeric
    qualities = filter(None, qualities)
    for sub in sequence:
        if sub == "-": ## If sequences have "-", it inserts a "0" in that position in corresponding number
            idx = list(sequence).index(sub)
            qualities.insert(idx,"0")

# Error in the steps below
pairs = []
for sub in l:
    print sub
    new_list = []
    for x in range(len(sequence)):
        print x
        new_tuple = (sequence[x], qualities[x]) #Printing this step, notice that only one of the sequences is printed twice. ERROR
        print new_tuple
        if int(qualities[x]) < 10:
            new_tuple = ("Z",   qualities[x]) 
        new_list.append(new_tuple)
    pairs.append(new_list)
print pairs
# When I print pairs it looks like this: [[('Z', '7'), ('Z', '7'), ('B', '28'), ('D', '29'), ('Z', '2'), ('Z', '0'), ('Z', '0')], [('Z', '7'), ('Z', '7'), ('B', '28'), ('D', '29'), ('Z', '2'), ('Z', '0'), ('Z', '0')]]
# Sequence#2 is printed twice over. The first one is not taken in

Tags: theinnewforiflinelistprint
1条回答
网友
1楼 · 发布于 2024-09-28 01:31:19
all_inputs = []  # <   add this
for i in range(0,ll):
    sq = l[i]
    sequence = sq.split(" ")[0] ## Stores only the alphabets
    qualities = sq.split(" ")[1:] ## Stores only the numeric
    qualities = filter(None, qualities)
    for sub in sequence:
        if sub == "-":
            idx = list(sequence).index(sub)
            qualities.insert(idx,"0")
    # also add this ***********************
    all_inputs.append((sequence, qualities))

pairs = []
# change this *******************************
for sequence, qualities in all_inputs:
    print sub 
    new_list = []
    for x in range(len(sequence)):
        print x
        new_tuple = (sequence[x], qualities[x]) 
        print new_tuple
        if int(qualities[x]) < 10: 
            new_tuple = ("Z",   qualities[x]) 
        new_list.append(new_tuple)
    pairs.append(new_list)
print pairs

提供:

[[('Z', '6'), ('Z', '6'), ('C', '35'), ('D', '25'), ('E', '10'), ('Z', '0'), ('Z', '0')], [('Z', '7'), ('Z', '7'), ('B
', '28'), ('D', '29'), ('Z', '2'), ('Z', '0'), ('Z', '0')]]

相关问题 更多 >

    热门问题