[Tutor] IndexError: index out of bounds
Sayan Chatterjee
sayanchatterjee at gmail.com
Wed Mar 27 18:36:01 CET 2013
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 <wprins at gmail.com> wrote:
> Hi Sayan,
>
> On 27 March 2013 16:31, Sayan Chatterjee <sayanchatterjee at 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
>
>
>
--
--------------------------------------------------------------------------
*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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130327/18c80ade/attachment-0001.html>
More information about the Tutor
mailing list