Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Strange array output?

import numpy as np
x=['a', 'b']
x=np.repeat(x,3)
x[0]="apple"

Why does my code produce ['a' 'a' 'a' 'b' 'b' 'b'] instead of ["apple" 'a' 'a' 'b' 'b' 'b']?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When you use numpy.repeat() your list will contain type numpy.str_ instead of str.
numpy.str_  is NumPy datatype used for arrays containing fixed-width byte strings. Check type(x[0]) and type ("apple") to see the difference.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading