4

Let's say i have a list like this one:

b = np.array(['a','b','c','a','b','c','a','b','c','a','b','c'])

and i wanted to insert this character at every 17th position '\n':

np.insert(b,b[::16],'\n')

why do i get this error message and how would be a corect way to do this?

ValueError: invalid literal for int() with base 10: 'a'

Thank you very much

1 Answer 1

4

The second argument for np.insert should be the index to place the values, you can try:

n = 3
np.insert(b, range(n, len(b), n), "\n") 

# array(['a', 'b', 'c', '\n', 'a', 'b', 'c', '\n', 'a', 'b', 'c', '\n', 'a',
#        'b', 'c'], 
#       dtype='<U1')
Sign up to request clarification or add additional context in comments.

1 Comment

Nice, thank you! I kinda misread numpy.insert needing a slice of indices ;)

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.