Newline (NuBe Question)
Roel Schroeven
roel at roelschroeven.net
Sun Nov 26 07:08:09 EST 2023
Michael F. Stemper via Python-list schreef op 25/11/2023 om 15:32:
> On 24/11/2023 21.45,avi.e.gross at gmail.com wrote:
> > Grizz[l]y,
> >
> > I think the point is not about a sorted list or sorting in general It is
> > about reasons why maintaining a data structure such as a list in a program
> > can be useful beyond printing things once. There are many possible examples
> > such as having a list of lists containing a record where the third item is a
> > GPA for the student and writing a little list comprehension that selects a
> > smaller list containing only students who are Magna Cum Laude or Summa Cum
> > Laude.
> >
> > studs = [
> > ["Peter", 82, 3.53],
> > ["Paul", 77, 2.83],
> > ["Mary", 103, 3.82]
> > ]
>
> > Of course, for serious work, some might suggest avoiding constructs like a
> > list of lists and switch to using modules and data structures [...]
>
> Those who would recommend that approach do not appear to include Mr.
> Rossum, who said:
>
> Avoid overengineering data structures. Tuples are better than
> objects (try namedtuple too though). Prefer simple fields over
> getter/setter functions... Built-in datatypes are your friends.
> Use more numbers, strings, tuples, lists, sets, dicts. Also
> check out the collections library, eps. deque.[1]
>
> I was nodding along with the people saying "list of lists" until I
> reread this quote. A list of tuples seems most appropriate to me.
I prefer namedtuples or dataclasses over tuples. They allow you to refer
to their fields by name instead of index: student.gpa is much clearer
than student[2], and makes it less likely to accidentally refer to the
wrong field.
--
"Man had always assumed that he was more intelligent than dolphins because
he had achieved so much — the wheel, New York, wars and so on — whilst all
the dolphins had ever done was muck about in the water having a good time.
But conversely, the dolphins had always believed that they were far more
intelligent than man — for precisely the same reasons."
-- Douglas Adams
More information about the Python-list
mailing list