How to insert a row into the numpy list of every nth index?

Let's say I have a list like this:

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

and I wanted to insert this character in every 17th position of '\ n':

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

why am I getting this error message and how can this be done differently?

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

Many thanks

+4
source share
1 answer

The second argument to np.insertshould be an 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')
+4
source

Source: https://habr.com/ru/post/1666929/


All Articles