0

Why when I'm executing code below I get those weird whitespaces in output?

import numpy as np

str = 'a a b c a a d a g a'
string_array = np.array(str.split(" "))
char_indices = np.where(string_array == 'a')
array = char_indices[0]
print(array)
array += 2
print(array)

output:

[0 1 4 5 7 9]
[ 2  3  6  7  9 11]

1 Answer 1

2

That's just numpy's way of displaying data to make it appear aligned and more readable.

The alignment between your two lists changes

[0 1 4 5 7 9]
[ 2  3  6  7  9 11]

because there is a two-digit element in the second list.

In vectors it is more difficult to appreciate, but it is very useful when we have more dimensions:

>>> a = np.random.uniform(0,1,(5,5))
>>> a[a>0.5] = 0
>>> print(a)
[[0.         0.         0.00460074 0.22880318 0.46584641]
 [0.0455245  0.         0.         0.         0.        ]
 [0.         0.07891556 0.21795357 0.14944522 0.20732431]
 [0.         0.         0.         0.3381172  0.08182367]
 [0.         0.         0.10734559 0.         0.31228533]]
>>> print(a.tolist())
[[0.0, 0.0, 0.0046007414146133074, 0.22880318354923768, 0.4658464110307319], [0.04552450444387102, 0.0, 0.0, 0.0, 0.0], [0.0, 0.07891556038021574, 0.21795356574892966, 0.1494452184954096, 0.2073243102108967], [0.0, 0.0, 0.0, 0.33811719550156627, 0.08182367499758836], [0.0, 0.0, 0.10734558995972832, 0.0, 0.31228532775003903]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.