Fixing ValueError: Setting Array Element w/ Sequence in Python

In summary, the conversation is about a person encountering an error when trying to use a specific code to convert a statement into a string/name and store it in an array. They are seeking advice on how to fix this error and successfully turn the statement into a string/name that can be stored in an array.
  • #1
vorcil
398
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
  • #2
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?
 
  • #3
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
 
  • #4
sorry about indentation, doesn't work when i copy / paste it for some reason
 
  • #5
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:
  • #6
vorcil said:
sorry about indentation, doesn't work when i copy / paste it for some reason

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

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

1. What does the "ValueError: Setting Array Element w/ Sequence" mean in Python?

The "ValueError: Setting Array Element w/ Sequence" is a common error that occurs when trying to assign a sequence, such as a list or tuple, to a single element of an array in Python. This error typically occurs when the size or shape of the sequence does not match the size or shape of the array.

2. How can I fix the "ValueError: Setting Array Element w/ Sequence" in my code?

There are a few potential solutions to fixing this error. One option is to make sure that the size and shape of your sequence matches that of the array you are trying to assign it to. Another option is to use the numpy.asarray() function to convert the sequence into an array before assigning it to the original array. You could also try using the numpy.append() function to add the sequence as a new element to the array.

3. Why am I getting the "ValueError: Setting Array Element w/ Sequence" when I didn't encounter it before?

This error can occur when the size or shape of your array or sequence has changed, such as when you are using a loop or function to modify the array. It could also be caused by accidentally modifying the size or shape of the array or sequence in your code. It is important to carefully check your code for any unintentional changes to the size or shape of your data.

4. Can I prevent the "ValueError: Setting Array Element w/ Sequence" from happening?

To prevent this error from occurring, it is important to carefully check the size and shape of your array and sequence before attempting to assign one to the other. You can also use the numpy.asarray() function to convert your sequence into an array if needed. Additionally, regularly checking your code for any unintentional changes to the size or shape of your data can help prevent this error from happening.

5. Are there any other similar errors that I should be aware of when working with arrays in Python?

Yes, there are a few other common errors that can occur when working with arrays in Python, such as "IndexError: index out of bounds" or "TypeError: list indices must be integers or slices, not tuple." These errors often occur when trying to access or modify elements in an array using improper indexing or data types. It is important to carefully check your code and make sure you are using the correct syntax when working with arrays in Python.

Similar threads

  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
28
Views
3K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
2K
Back
Top