简体   繁体   中英

How to use enumerate in a list comprehension with two lists?

I just started to use list comprehension and I'm struggling with it. In this case, I need to get the n number of each list ( sequence_0 and sequence_1 ) that the iteration is at each time. How can I do that?

The idea is to get the longest sequence of equal nucleotides (a motif) between the two sequences. Once a pair is finded, the program should continue in the nexts nucleotides of the sequences, checking if they are also equal and then elonganting the motif with it. The final output should be an list of all the motifs finded. The problem is, to continue in the next nucleotides once a pair is finded, i need the position of the pair in both sequences to the program continue. The index function does not work in this case, and that's why i need the enumerate.

Also, I don't understand exactly the reason for the x and y between () , it would be good to understand that too:)

just to explain, the content of the lists is DNA sequences, so its basically something like:

sequence_1 = ['A', 'T', 'C', 'A', 'C']
def find_shared_motif(arq):
    data = fastaread(arq)
    seqs = [list(sequence) for sequence in data.values()]
    motifs = [[]]
    i = 0
    sequence_0, sequence_1 = seqs[0], seqs[1]  # just to simplify

    for x, y in [(x, y) for x in zip(sequence_0[::], sequence_0[1::]) for y in zip(sequence_1[::], sequence_1[1::])]:
        print(f'Pairs {"".join(x)} and {"".join(y)} being analyzed...')
        if x == y:
            print(f'Pairs {"".join(x)} and {"".join(y)} match!')
            motifs[i].append(x[0]), motifs[i].append(x[1])
            k = sequence_0.index(x[0]) + 2  # NAO ESTA DEVOLVENDO O NUMERO CERTO
            u = sequence_1.index(y[0]) + 2
            print(k, u)

            # Determines if the rest of the sequence is compatible
            print(f'Starting to elongate the motif {x}...')
            for j, m in enumerate(sequence_1[u::]):
                try:
                    # Checks if the nucleotide is equal for both of the sequences
                    print(f'Analyzing the pair {sequence_0[k + j]}, {m}')
                    if m == sequence_0[k + j]:
                        motifs[i].append(m)
                        print(f'The pair {sequence_0[k + j]}, {m} is equal!')

                    # Stop in the first nonequal residue
                    else:
                        print(f'The pair {sequence_0[k + j]}, {m} is not equal.')
                        break

                except IndexError:
                    print('IndexError, end of the string')

        else:
            i += 1
            motifs.append([])

        return motifs
...

One way to go with it is to start zipping both lists:

a = ['A', 'T', 'C', 'A', 'C']
b = ['A', 'T', 'C', 'C', 'T']

c = zip(a,b)

In that case, c will have the values below

c = [('A','A'), ('T','T'), ('C','C'), ('A','C'), ('C','T')]

Then, it is obvious that what you want is to compare the first element with the tuple with the second one. But, also, you will need the position where the difference resides. So, you can go with enumerate :

for i, t in enumerate(c):
    if c[0] != c[1]:
        return i
    return -1

In that way, if you get -1 at the end, it means that all elements in both lists are equal, side by side. Otherwise, you will get the position of the difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM