[Tutor] dynamic arrays?
Alex Hall
mehgcap at gmail.com
Mon Sep 27 17:54:47 CEST 2010
On 9/27/10, Brian Jones <bkjones at gmail.com> wrote:
> On Mon, Sep 27, 2010 at 11:39 AM, Alex Hall <mehgcap at gmail.com> wrote:
>
>> On 9/27/10, Brian Jones <bkjones at gmail.com> wrote:
>> > On Mon, Sep 27, 2010 at 11:32 AM, Alex Hall <mehgcap at gmail.com> wrote:
>> >
>> >> Hi all,
>> >> One thing I have never much liked about Python is its need for
>> >> specifically sized arrays and lack of a dynamic, array-like data
>> >> structure. For example, the following fails with a "list assignment
>> >> index out of range" error:
>> >>
>> >> a=[]
>> >> i=0
>> >> for l in open("file.txt", "r"):
>> >> a[i]=l
>> >> i+=1
>> >>
>> >
>> > Is there some reason to use this construct rather than the list object's
>> > 'append' method?
>> >
>> > for i in open('file.txt', 'r'):
>> > a.append(l)
>>
>> Ah, good thought. So exactly what are the functional differences
>> between a list and an array in Python, or are they so close that it
>> makes no difference which you use? It seems like you can index into
>> both with the bracket notation. Are lists limited in any way that
>> makes them not as desirable as arrays?
>>
>
> A python list and a python array are one and the same to my knowledge. Do
> you mean a list and a dict? A dict is an unordered collection of key-value
> pairs, and the keys are more or less arbitrary values of your own creation
> (I believe the single limitation is the key must be a hashable type). A list
> is a 'flat' array, although you can use enumerate(l) to get index->value
> pairs from a list 'l'.
Oh. Somehow I never made the connection that arrays and lists are the
same. :) No, I was not thinking of a dictionary in this case, though I
will have to look into that enumerate function. Thanks for clearing
this up.
>
> hth.
>
>
>
>> >
>> > brian
>> >
>> >>
>> >> Is there something in Python I am missing that would let the above
>> >> work? I am hoping that my annoyance at the apparent lack of such a
>> >> thing is unfounded. BTW, I know why the above throws that exception.
>> >> TIA.
>> >>
>> >> --
>> >> Have a great day,
>> >> Alex (msg sent from GMail website)
>> >> mehgcap at gmail.com; http://www.facebook.com/mehgcap
>> >> _______________________________________________
>> >> Tutor maillist - Tutor at python.org
>> >> To unsubscribe or change subscription options:
>> >> http://mail.python.org/mailman/listinfo/tutor
>> >>
>> >
>> >
>> >
>> > --
>> > Brian K. Jones
>> > My Blog http://www.protocolostomy.com
>> > Follow me http://twitter.com/bkjones
>> >
>>
>>
>> --
>> Have a great day,
>> Alex (msg sent from GMail website)
>> mehgcap at gmail.com; http://www.facebook.com/mehgcap
>>
>
>
>
> --
> Brian K. Jones
> My Blog http://www.protocolostomy.com
> Follow me http://twitter.com/bkjones
>
--
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap
More information about the Tutor
mailing list