One-time calculation loop
A list of integers that represents each Pokémon's generation has been loaded into your session called generations. You'd like to gather the counts of each generation and determine what percentage each generation accounts for out of the total count of integers.
The below loop was written to accomplish this task:
for gen,count in gen_counts.items():
total_count = len(generations)
gen_percent = round(count / total_count * 100, 2)
print(
'generation {}: count = {:3} percentage = {}'
.format(gen, count, gen_percent)
)
Let's make this loop more efficient by moving a one-time calculation outside the loop.
Latihan ini adalah bagian dari kursus
Writing Efficient Python Code
Petunjuk latihan
- Import
Counterfrom thecollectionsmodule. - Use
Counter()to collect the count of each generation from thegenerationslist. Save this asgen_counts. - Write a better for loop that places a one-time calculation outside (above) the loop. Use the exact same syntax as the original for loop (simply copy and paste the one-time calculation above the loop).
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# Import Counter
____
# Collect the count of each generation
gen_counts = ____
# Improve for loop by moving one calculation above the loop
total_count = ____
for gen,count in gen_counts.items():
gen_percent = round(count / total_count * 100, 2)
print('generation {}: count = {:3} percentage = {}'
.format(gen, count, gen_percent))