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

Discussion Overview

The discussion revolves around a Python programming issue related to a ValueError encountered when attempting to assign formatted strings to elements of a numpy array. The participants explore the nature of the error and seek solutions for properly storing formatted string values in an array.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to format a string and assign it to an array, which results in a ValueError.
  • Another participant mentions that the code works in Python 3.x, suggesting that the issue may be related to the initialization of the array and requests a complete code snippet for better understanding.
  • A participant provides a function that attempts to create formatted names based on row and column indices but does not clarify the type of the array being used.
  • One participant points out that the main issue is trying to store strings in a numpy numeric array, indicating that this is not permissible.
  • Another participant suggests using proper formatting tags for code to improve readability, acknowledging issues with indentation when copying code.

Areas of Agreement / Disagreement

Participants express differing views on the proper way to handle the array initialization and the type of data that can be stored in numpy arrays. There is no consensus on a solution to the problem presented.

Contextual Notes

There are unresolved issues regarding the initialization of the numpy array and the intended data types for the elements being stored. The discussion lacks clarity on the complete context of the code and the specific requirements for the array.

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
4K
  • · 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
3K
  • · Replies 19 ·
Replies
19
Views
7K