[Tutor] IndexError: index out of bounds
Dear all, When trying to print or assign array elements, getting the following error: Traceback (most recent call last): File ZA.py, line 32, in module p_za[i] = p_initial[i] + t*K*cos(K*p_initial[i]); IndexError: index out of bounds I am using Numpy, is it due to that? I am attaching the code herewith. -- -- *Sayan Chatterjee* Dept. of Physics and Meteorology IIT Kharagpur Lal Bahadur Shastry Hall of Residence Room AB 205 Mob: +91 9874513565 blog: www.blissprofound.blogspot.com Volunteer , Padakshep www.padakshep.org ZA.py Description: Binary data ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError: index out of bounds
On Wed, Mar 27, 2013 at 11:50 AM, Sayan Chatterjee sayanchatter...@gmail.com wrote: Dear all, When trying to print or assign array elements, getting the following error: Traceback (most recent call last): File ZA.py, line 32, in module p_za[i] = p_initial[i] + t*K*cos(K*p_initial[i]); You declare p_za = [] above. So there is no p_za[i]. You should use append since you are adding elements to the end of the list. IndexError: index out of bounds I am using Numpy, is it due to that? I am attaching the code herewith. -- -- *Sayan Chatterjee* Dept. of Physics and Meteorology IIT Kharagpur Lal Bahadur Shastry Hall of Residence Room AB 205 Mob: +91 9874513565 blog: www.blissprofound.blogspot.com Volunteer , Padakshep www.padakshep.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError: index out of bounds
Hi, On 27 March 2013 15:50, Sayan Chatterjee sayanchatter...@gmail.com wrote: Dear all, When trying to print or assign array elements, getting the following error: Traceback (most recent call last): File ZA.py, line 32, in module p_za[i] = p_initial[i] + t*K*cos(K*p_initial[i]); IndexError: index out of bounds I am using Numpy, is it due to that? I am attaching the code herewith. Not Numpy no. The p_za list appears to be empty at the point where the above assgnment to p_za[i] is done, hence you get the IndexError message. You should initialise p_za to be as long as needed first. Maybe by simply using p_za = [None] * N instead of assigning [], or alternately perhaps by appending instead at the point where you first reference p_ze[i]. Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError: index out of bounds
p_za = [None]*N is not giving away the error message. for i in range(0,N): p_za.append = p_initial[i] + t*K*cos(K*p_initial[i]); is also not working. Could you please redirect me to a link where the example is demonstrated? What is the simplest way to assign an array element a value? i.e the C analogue of: int array[200] for(i=0;i200;i++) array[i] = 2*i + 5; On 27 March 2013 21:44, Walter Prins wpr...@gmail.com wrote: Hi, On 27 March 2013 15:50, Sayan Chatterjee sayanchatter...@gmail.comwrote: Dear all, When trying to print or assign array elements, getting the following error: Traceback (most recent call last): File ZA.py, line 32, in module p_za[i] = p_initial[i] + t*K*cos(K*p_initial[i]); IndexError: index out of bounds I am using Numpy, is it due to that? I am attaching the code herewith. Not Numpy no. The p_za list appears to be empty at the point where the above assgnment to p_za[i] is done, hence you get the IndexError message. You should initialise p_za to be as long as needed first. Maybe by simply using p_za = [None] * N instead of assigning [], or alternately perhaps by appending instead at the point where you first reference p_ze[i]. Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- -- *Sayan Chatterjee* Dept. of Physics and Meteorology IIT Kharagpur Lal Bahadur Shastry Hall of Residence Room AB 205 Mob: +91 9874513565 blog: www.blissprofound.blogspot.com Volunteer , Padakshep www.padakshep.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError: index out of bounds
Hi Sayan, On 27 March 2013 16:31, Sayan Chatterjee sayanchatter...@gmail.com wrote: p_za = [None]*N is not giving away the error message. for i in range(0,N): p_za.append = p_initial[i] + t*K*cos(K*p_initial[i]); is also not working. append() is a method, so using append you want something like: for i in range(0,N): p_za.append( p_initial[i] + t*K*cos(K*p_initial[i]) ); After every loop iteration, the list grows by having one item appended to it, being the result of the expression: p_initial[i] + t*K*cos(K*p_initial[i]) Could you please redirect me to a link where the example is demonstrated? http://courses.cms.caltech.edu/cs11/material/python/misc/python_idioms.html See the paragraph on Sequence multiplication. What is the simplest way to assign an array element a value? What you have is fine for assignment to a particular slot in the list. What you've missed and has already been pointed out, is to initialise/set the length of your list first, before trying to set the value of arbitrary slots. In the C example you posted the array is declared with length 200 up front. In your Python code however you assign [], which is a list of length 0. By contrast, the expression I gave you before, e.g. [None] * N, generates a list of length N, with each element in the list being the None object, thus initialising the list, ensuring that you can later assign to arbitrary slots when needed. Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError: index out of bounds
Sayan Chatterjee wrote: When trying to print or assign array elements, getting the following error: Traceback (most recent call last): File ZA.py, line 32, in module p_za[i] = p_initial[i] + t*K*cos(K*p_initial[i]); IndexError: index out of bounds I am using Numpy, is it due to that? I am attaching the code herewith. If you are using numpy it is likely that you don't need to loop over the index explicitly. Assuming t and K are scalars, and p_initial is a numpy array you can write p_za = p_initial + t * K * numpy.cos(K*p_initial) For example: import numpy p_initial = numpy.array([1.2, 3.4, 5.6]) t = 1.1 K = 2.2 p_initial + t*K*numpy.cos(K*p_initial) array([-0.92189929, 4.28408588, 7.94692559]) Quite powerful, once you get the knack of it. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError: index out of bounds
Hi Walter, Thanks a lot! Yes, now I get your point. append is working perfectly fine. Hi Peter: Exactly. It's very nice. Indices needn't have to be mentioned explicitly. No explicit looping and the thing is done! But I have a question, whenever we want to do operations on the individual array elements, don't we have to mention the indices explicitly i.e p_za[i]? 1) Traceback (most recent call last): File ZA.py, line 44, in module p_za = p_za % 4 TypeError: unsupported operand type(s) for %: 'list' and 'int' 2) Traceback (most recent call last): File ZA.py, line 43, in module if p_za[i] 4.0: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() When the i indices are removed * (1) * error message is showing up and when i is included *(2) *is shown.* * On 27 March 2013 22:29, Walter Prins wpr...@gmail.com wrote: Hi Sayan, On 27 March 2013 16:31, Sayan Chatterjee sayanchatter...@gmail.comwrote: p_za = [None]*N is not giving away the error message. for i in range(0,N): p_za.append = p_initial[i] + t*K*cos(K*p_initial[i]); is also not working. append() is a method, so using append you want something like: for i in range(0,N): p_za.append( p_initial[i] + t*K*cos(K*p_initial[i]) ); After every loop iteration, the list grows by having one item appended to it, being the result of the expression: p_initial[i] + t*K*cos(K*p_initial[i]) Could you please redirect me to a link where the example is demonstrated? http://courses.cms.caltech.edu/cs11/material/python/misc/python_idioms.html See the paragraph on Sequence multiplication. What is the simplest way to assign an array element a value? What you have is fine for assignment to a particular slot in the list. What you've missed and has already been pointed out, is to initialise/set the length of your list first, before trying to set the value of arbitrary slots. In the C example you posted the array is declared with length 200 up front. In your Python code however you assign [], which is a list of length 0. By contrast, the expression I gave you before, e.g. [None] * N, generates a list of length N, with each element in the list being the None object, thus initialising the list, ensuring that you can later assign to arbitrary slots when needed. Walter -- -- *Sayan Chatterjee* Dept. of Physics and Meteorology IIT Kharagpur Lal Bahadur Shastry Hall of Residence Room AB 205 Mob: +91 9874513565 blog: www.blissprofound.blogspot.com Volunteer , Padakshep www.padakshep.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError: index out of bounds
Sayan Chatterjee wrote: Hi Walter, Thanks a lot! Yes, now I get your point. append is working perfectly fine. Hi Peter: Exactly. It's very nice. Indices needn't have to be mentioned explicitly. No explicit looping and the thing is done! But I have a question, whenever we want to do operations on the individual array elements, don't we have to mention the indices explicitly i.e p_za[i]? For Python's built-in list, yes, but not for numpy arrays. 1) Traceback (most recent call last): File ZA.py, line 44, in module p_za = p_za % 4 TypeError: unsupported operand type(s) for %: 'list' and 'int' items = [3, 4, 5] items % 2 Traceback (most recent call last): File stdin, line 1, in module TypeError: unsupported operand type(s) for %: 'list' and 'int' items = numpy.array(items) items % 2 array([1, 0, 1]) Even for lists you can do better than using an explicit index, you can iterate over its members: items = [3, 4, 5] [v % 2 for v in items] [1, 0, 1] 2) Traceback (most recent call last): File ZA.py, line 43, in module if p_za[i] 4.0: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() When the i indices are removed * (1) * error message is showing up and when i is included *(2) *is shown.* * You are probably seeing that error because p_za[i] is a numpy.array, i. e. you have a list of arrays: items = [numpy.array([1,2]), numpy.array([3,4])] items[0] 4 array([False, False], dtype=bool) if items[0] 4: pass ... Traceback (most recent call last): File stdin, line 1, in module ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() How you managed to get there and what you actually want to achieve -- I can't tell from what you provide. Perhaps you can give a little more context, in code, but more importantly in prose. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError: index out of bounds
On 27/03/13 17:36, Sayan Chatterjee wrote: 2) Traceback (most recent call last): File ZA.py, line 43, in module if p_za[i] 4.0: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() This implies that p_za[i] is actually an array. So maybe p_za is a list (of arrays)? Try printing it to see. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError: index out of bounds
Hi Peter, Thanks!! Yes, when handled as a numpy array, it's working fine! Traceback (most recent call last): File ZA.py, line 59, in module if temp_za == j: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() This error occurs when the temp_za ( a numpy array, print temp_za works fine) is compared with j. I am attaching the code. Another question how do I get an integer value for p_za / 2 . Type casting with int says: TypeError: only length-1 arrays can be converted to Python scalars. On 27 March 2013 23:37, Alan Gauld alan.ga...@btinternet.com wrote: On 27/03/13 17:36, Sayan Chatterjee wrote: 2) Traceback (most recent call last): File ZA.py, line 43, in module if p_za[i] 4.0: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() This implies that p_za[i] is actually an array. So maybe p_za is a list (of arrays)? Try printing it to see. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ __**_ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor -- -- *Sayan Chatterjee* Dept. of Physics and Meteorology IIT Kharagpur Lal Bahadur Shastry Hall of Residence Room AB 205 Mob: +91 9874513565 blog: www.blissprofound.blogspot.com Volunteer , Padakshep www.padakshep.org ZA.py Description: Binary data ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError: index out of bounds
Sayan Chatterjee wrote: Yes, when handled as a numpy array, it's working fine! Traceback (most recent call last): File ZA.py, line 59, in module if temp_za == j: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() From the attached script: if temp_za == j: counter += 1 Do you want to count the entries? temp_za = numpy.array([1, 2, 3, 2, 1, 1]) (temp_za == 1).sum() 3 This error occurs when the temp_za ( a numpy array, print temp_za works fine) is compared with j. I am attaching the code. I'm sorry, I am lacking the domain knowledge to make sense of it. Just one more remark: [] 0.0 is always False, regardless of the contents of the list Another question how do I get an integer value for p_za / 2 . Type casting with int says: TypeError: only length-1 arrays can be converted to Python scalars. I'd use numpy.array(p_za//2, dtype=int) but I'm not a numpy expert. As you dig deeper the tutor mailing list may not be the best place to ask -- numpy has a dedicated mailing list of its own. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor