Anonymous
Anonymous

Reputation:

How to add a counter in a list comprehension expression?

Is it possible to do a list comprehension expression on the following?

end_pieces = ['ATCGCTGCAATG', 'CCTACGGCATGG', 'AGTACTTTATTA', 'TAACAGTTCTTC']

genome_ht = {'ATCGCTGCAATG': [18], 'CCTACGGCATGG': [30], 'AGTACTTTATTA': [42]}


start_positions = []
count = 0
for read_piece in end_pieces:
    for index in genome_ht[read_piece]:
        start_positions.append(index-(count*KEY_LENGTH))
    count +=1


 >>> print start_positions
 [18, 18, 18]

Upvotes: 0

Views: 1679

Answers (2)

maahl
maahl

Reputation: 547

You can do it with a double iteration in list comprehension, and the function enumerate:

start_positions = [index - count * KEY_LENGTH for count, read_piece in enumerate(end_pieces) for index in genome_ht[read_piece]]

Upvotes: 0

Łukasz Rogalski
Łukasz Rogalski

Reputation: 23203

enumerate allows you to count without local variable. Besides that, it's simple nested iteration over two sequences.

end_pieces = ['ATCGCTGCAATG', 'CCTACGGCATGG', 'AGTACTTTATTA', 'TAACAGTTCTTC']
genome_ht = {'ATCGCTGCAATG': [18], 'CCTACGGCATGG': [30], 'AGTACTTTATTA': [42], 'TAACAGTTCTTC': [1]}

KEY_LENGTH = 12
start_positions = [index-count*KEY_LENGTH for count, read_piece in enumerate(end_pieces) for index in genome_ht[read_piece]]

Upvotes: 1

Related Questions