Home > Enterprise >  How can I assign a strin to an element in numpy arrays
How can I assign a strin to an element in numpy arrays

Time:10-28

When I assign strings to the array it just picks up the first character and I need the entire string. Am I using the wrong method?

import numpy

i=0

def func(name,number,array,i):
    arry[i,0]=number
    array[i,1]=name
    print(array)

People= numpy.zeros([5,2],dtype=str)

func("qwe","123",People,i)

#this is the output    
 [['1' 'q']
 ['' '']
 ['' '']
 ['' '']
 ['' '']]
#this is the desired output
[['123' 'qwe']
 ['' '']
 ['' '']
 ['' '']
 ['' '']]

CodePudding user response:

Assign the people array to hold objects instead of strings:

People= numpy.zeros([5,2], dtype=object)

print(func("qwe","123",People,i))
# [['123' 'qwe']
#  ['' '']
#  ['' '']
#  ['' '']
#  ['' '']]
  • Related