Reading a file into a data structure....
Ian Kelly
ian.g.kelly at gmail.com
Sun Oct 16 06:15:17 EDT 2011
On Sat, Oct 15, 2011 at 8:18 PM, MrPink <tdsimpson at gmail.com> wrote:
> I did not understand what a tuple was.
> So it was very hard for me to understand what a namedtuple was and
> follow the code.
> Then I looked up the word at dictionary.com and got this:
> http://dictionary.reference.com/browse/tuple
>
> tuple: computing a row of values in a relational database
>
> Now I understand a little better what a tuple is and can follow the
> code better.
Python tuples do not have anything to do with relational databases.
You would get a better introduction from Wikipedia:
http://en.wikipedia.org/wiki/Tuple
In Python, tuples are nothing more than immutable sequences, as
opposed to lists, which are mutable sequences. Another way of
characterizing the difference between lists and tuples is in how they
are typically used. A list is typically used for a homogeneous
(meaning all elements are treated in the same way) sequence containing
an arbitrary number of unrelated objects. A tuple is typically used
for a heterogeneous sequence of a certain length. For example, a
tuple might be expected to contain exactly two strings and an int that
are related in some fashion.
> A namedtuple seems like a dictionary type. I'll need to read up on
> the difference between the two.
A namedtuple is a tuple subclass where each of the elements the tuple
is expected to contain has been given a specific name for ease of
reference. The names are essentially aliases for numerical indices.
It differs from a dictionary in that it is ordered and only contains
elements with specific names (and always contains those elements),
whereas a dictionary contains arbitrary key-value pairs.
More information about the Python-list
mailing list