This question already has an answer here:
I was learning Scientific Programing with Python and found this single line if-for combination (To count the number of times a letter is in a string):
def count_v11(dna, base):
return len([i for i in range(len(dna)) if dna[i] == base])
Where dna is a string combination of 'ATGC' and base just a single string. Example:
dna = 'ATGCGGACCTAT'
base = 'C'
That should return 3 in this case.
As I understand, in the cases of if of single lines is
a = (value1 if condition else value2)
and the for are
newlist = [E(e) for e in list]
I would think that the if is evaluated first before the for, but in that case, there would be no i to evaluate the condition dna[i]==base because the for is not running yet. I'm trying to think how that code would be if the if and for where written in blocks instead of that compact way to understand it better, but I cant find how.
Can someone please write this in block to understand it?
Aucun commentaire:
Enregistrer un commentaire