0

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]

2 Answers2

1

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]]
Łukasz Rogalski
  • 18,883
  • 6
  • 53
  • 84
0

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]]
Community
  • 1
  • 1
maahl
  • 505
  • 2
  • 16