ÎncepețiÎncepe gratuit

Totul laolaltă: profilarea universului Star Wars

O listă cu 480 de supereroi a fost încărcată în sesiunea ta (numită heroes), împreună cu o listă a editorului corespunzător fiecărui erou (numită publishers).

Vrei să filtrezi lista heroes în funcție de editorul unui erou, dar nu ești sigur care dintre funcțiile de mai jos este mai eficientă.

def get_publisher_heroes(heroes, publishers, desired_publisher):

    desired_heroes = []

    for i,pub in enumerate(publishers):
        if pub == desired_publisher:
            desired_heroes.append(heroes[i])

    return desired_heroes
def get_publisher_heroes_np(heroes, publishers, desired_publisher):

    heroes_np = np.array(heroes)
    pubs_np = np.array(publishers)

    desired_heroes = heroes_np[pubs_np == desired_publisher]

    return desired_heroes

Acest exercițiu face parte din cursul

Scriere eficientă a codului Python

Vezi cursul

Exercițiu interactiv practic

Încearcă acest exercițiu completând acest cod de exemplu.

# Use get_publisher_heroes() to gather Star Wars heroes
star_wars_heroes = get_publisher_heroes(____, ____, ____)

print(star_wars_heroes)
print(type(star_wars_heroes))

# Use get_publisher_heroes_np() to gather Star Wars heroes
star_wars_heroes_np = get_publisher_heroes_np(____, ____, ____)

print(star_wars_heroes_np)
print(type(star_wars_heroes_np))
Editează și rulează codul