Fixing ValueError: Setting Array Element w/ Sequence in Python

  • Context: Python 
  • Thread starter Thread starter vorcil
  • Start date Start date
  • Tags Tags
    Array Error Python
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
5 replies · 3K views
vorcil
Messages
395
Reaction score
0

Homework Statement



fname='%05s - %05s . %05s - %05s' % (xstart,xend,ystart,yend)
gives the error
ValueError: setting an array element with a sequence.

Basically I wanted to turn the statement '%05s - %05s . %05s - %05s' % ('50','50','50','50') to a string/name
that goes into an array of names

how?
Cheers

Homework Equations


The Attempt at a Solution

 
Physics news on Phys.org
This works fine for me in python 3.x. Of course, I did have to initialize the array with prior data. I used i=2. Perhaps you could post a compete chunk of code that fails?
 
def makeFName(row,col,res):

mod = row%res
if(mod !=0):
j=((row-mod)/res)+1
else:
j=row/res
mod=col%res1
if(mod !=0):
k=((col-mod)/res)+1
else:
k=col/res

fname=numpy.zeros((1,j*k))
print fname
xstart=1
ystart=1
i=0


while(xstart < row):
while(ystart < col):
fname='%05d - %05d . %05d - %05d' % (xstart,
10,
10,
10)
ystart+=res
i+=1
xstart+=res
print fname
 
sorry about indentation, doesn't work when i copy / paste it for some reason
 
Your main problem is that you are trying to put strings into a numpy numeric array. You can't do that. I don't know why you would want to initialize an array with numpy to begin with if you want to store strings. There are a number of other things I don't understand either. But that's the first thing that catches my attention.
 
Last edited:
vorcil said:
sorry about indentation, doesn't work when i copy / paste it for some reason

Surround your code by [noparse]
Code:
[/noparse] tags.