Python Fixing ValueError: Setting Array Element w/ Sequence in Python

AI Thread Summary
The discussion centers on a Python error encountered when trying to assign formatted strings to a NumPy array. The error, "ValueError: setting an array element with a sequence," arises because the user is attempting to insert strings into a numeric NumPy array, which is incompatible. The solution suggested involves using a different data structure, such as a list or a NumPy array specifically designed for strings, rather than a numeric array. Additionally, there is a request for a complete code snippet to better diagnose the issue. The importance of proper initialization and the correct handling of data types in arrays is emphasized, highlighting that strings cannot be stored in numeric arrays.
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

 
Technology 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top