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
Click For Summary
SUMMARY

The discussion addresses the ValueError encountered when attempting to assign a formatted string to a numeric NumPy array in Python. The error arises because NumPy arrays are designed to hold elements of a single data type, and strings cannot be assigned to a numeric array. The solution involves using a NumPy array of type 'object' or switching to a list to store the formatted strings. The user is advised to initialize the array correctly and ensure that the data types are compatible.

PREREQUISITES
  • Understanding of Python 3.x syntax and data types
  • Familiarity with NumPy arrays and their data type constraints
  • Knowledge of string formatting in Python
  • Basic programming concepts such as loops and conditionals
NEXT STEPS
  • Learn how to create NumPy arrays with different data types using numpy.array()
  • Explore the use of Python lists for dynamic data storage
  • Investigate string formatting techniques in Python, specifically using % and str.format()
  • Study error handling in Python to manage exceptions like ValueError
USEFUL FOR

This discussion is beneficial for Python developers, particularly those working with NumPy for data manipulation, and anyone encountering type-related errors in their code.

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.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 28 ·
Replies
28
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
7K