below codes purpose is how many times a specific string occurs consecutively in a given string. But I could not understand the logic of [sum(1 for _ in group)+1 for label, group in groups if label==''][0]
. I am looking for an explanation. I am writing what I understood so that you can correct me. Any help and explanation is highly appreciated thank you for your time.
from sum(1 for _ in group)+1
: Sum 1s for anything that is in group
but I think like group
is not defined, I don't know if it is something that comes with the library but it is not colored.
from [sum(1 for _ in group)+1 for label, group in groups if label==''][0]
I basically can not follow, if label is a empty string but I don't know about [0]
at the end.
from itertools import groupby
checkstr = ['AGATC', 'AATG', 'TATC']
s = 'GCTAAATTTGTTCAGCCAGATGTAGGCTTACAAATCAAGCTGTCCGCTCGGCACGGCCTACACACGTCGTGTAACTACAACAGCTAGTTAATCTGGATATCACCATGACCGAATCATAGATTTCGCCTTAAGGAGCTTTACCATGGCTTGGGATCCAATACTAAGGGCTCGACCTAGGCGAATGAGTTTCAGGTTGGCAATCAGCAACGCTCGCCATCCGGACGACGGCTTACAGTTAGTAGCATAGTACGCGATTTTCGGGAAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGTATCTATCTATCTATCTATCT'
for c in checkstr:
groups = groupby(s.split(c))
try:
print(c,[sum(1 for _ in group)+1 for label, group in groups if label==''][0])
except IndexError:
print(c,0)
print(sum(1 for _ in group)+1)