Pokémon totals and averages without a loop
A list of 720 Pokémon has been loaded into your session called names. Each Pokémon's corresponding statistics has been loaded as a NumPy array called stats. Each row of stats corresponds to a Pokémon in names and each column represents an individual Pokémon stat (HP, Attack, Defense, Special Attack, Special Defense, and Speed respectively.)
You want to gather each Pokémon's total stat value (i.e., the sum of each row in stats) and each Pokémon's average stat value (i.e., the mean of each row in stats) so that you find the strongest Pokémon.
The below for loop was written to collect these values:
poke_list = []
for pokemon,row in zip(names, stats):
total_stats = np.sum(row)
avg_stats = np.mean(row)
poke_list.append((pokemon, total_stats, avg_stats))
Latihan ini adalah bagian dari kursus
Writing Efficient Python Code
Petunjuk latihan
- Replace the above for loop using NumPy:
- Create a total stats array (
total_stats_np) using the.sum()method and specifying the correct axis. - Create an average stats array (
avg_stats_np) using the.mean()method and specifying the correct axis. - Create a final output list (
poke_list_np) by combining thenameslist, thetotal_stats_nparray, and theavg_stats_nparray.
- Create a total stats array (
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# Create a total stats array
total_stats_np = ____.____(axis=____)
# Create an average stats array
avg_stats_np = ____
# Combine names, total_stats_np, and avg_stats_np into a list
poke_list_np = [*____(names, total_stats_np, avg_stats_np)]
print(poke_list_np == poke_list, '\n')
print(poke_list_np[:3])
print(poke_list[:3], '\n')
top_3 = sorted(poke_list_np, key=lambda x: x[1], reverse=True)[:3]
print('3 strongest Pokémon:\n{}'.format(top_3))