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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top