Array indexing

Download notebook Interact

These exercises follow on from the array indexing page.

Run this cell to start:

import numpy as np

So far we have seen arrays that contain numbers. Here is an array that contains strings:

some_words = np.array(['to', 'be', 'or', 'not', 'to', 'be'])
some_words
array(['to', 'be', 'or', 'not', 'to', 'be'], dtype='<U3')

Indexing with integers

Use indexing with integers to display the first word in some_words.

# Your code here

Use indexing with integers to display the word “not”.

# Your code here

Here is a Boolean array that has True where the word in the matching position of some_words is “be”:

bees = some_words == 'be'
bees
array([False,  True, False, False, False,  True])

Use Boolean indexing to show an array with the two instances of “be” from some_words.

# Your code here

Use Boolean indexing to show an array with the two instances of “to” from some_words.

# Your code here

Use Boolean indexing to show an array with the single instance of “not” from some_words.

# Your code here