python - For Loop questions -
i'm used seeing for loops in format:
for number in l: sum = sum + number
i browsing forums , came across piece of code:
count_chars = ".arpz" string = "phillip s. doing job." counts = tuple(string.count(d) for(d) in count_chars) print counts i'm not sure if for loop, decided rewrite in way understood:
tuple( for(d) in count_chars: string.count(d)) needless say, failed lol. can explain going on, , explain folly of logic? thanks!!
it's not quite for loop such, generator expression. return iterator each element amount of time every character in count_chars occurs in d. adds of these elements tuple.
it (roughly) equivalent to:
counts = [] d in count_chars: counts.append(string.count(d)) counts = tuple(counts)
Comments
Post a Comment