Mailing List Archive

Newline (NuBe Question)
Hi & thanks for patience with what could be simple to you

Have this (from an online "classes" tutorial)

--- Start Code Snippit ---

students = []
grades = []
for s in geographyClass:
students.append(geographyStudent(s))
for s in students:
grades.append(s.school)
grades.append(s.name)
grades.append(s.finalGrade())
if s.finalGrade()>82:
grades.append("Pass")
else:
grades.append("Fail")
print(grades)

--- End Code Snippit ---

I have extended (from tutorial) it a bit, I would really like to have a newline

at end of each record, I have searched (and tested) but cant get "\n" to give a

newline, I get "Mydata\n"

Do I need to replace "append" with "print", or is there a way to get the
newline in as I append to list?

Thanks again
--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
On 15Nov2023 07:25, Grizzy Adams <RealGrizzlyAdams@vivaldi.net> wrote:
>Have this (from an online "classes" tutorial)

Response inline below.

>students = []
>grades = []
>for s in geographyClass:
> students.append(geographyStudent(s))
>for s in students:
> grades.append(s.school)
> grades.append(s.name)
> grades.append(s.finalGrade())
> if s.finalGrade()>82:
> grades.append("Pass")
> else:
> grades.append("Fail")
>print(grades)
>
>--- End Code Snippit ---
>
>I have extended (from tutorial) it a bit, I would really like to have a newline
>at end of each record, I have searched (and tested) but cant get "\n"
>to give a newline, I get "Mydata\n"

It would be useful to:
- see some of the things you've tried
- what their output actually was
- what output you actually want to achieve

>Do I need to replace "append" with "print", or is there a way to get the
>newline in as I append to list?

I think you're confusing output (print) with your data (the list of
grades).

Is the code above genuinely what you're running?

I ask because it looks to me that you're:
- appending all the individual grade fields (school, name, ...) to one
long grades list containing all the data from all the students
- you're printing that single enormous list in one go at the end

What I'm imagine you want is one line of grade information per student.

Remember that indentation is important in Python. You're grades code
looks like this:

grades = []
for s in students:
grades.append(s.school)
grades.append(s.name)
grades.append(s.finalGrade())
if s.finalGrade()>82:
grades.append("Pass")
else:
grades.append("Fail")
print(grades)

This:
- makes an empty list
- gathers up all of the student data
- prints the data in one go

I think you may want to do the first and last steps on a per student
basis, not just once at the start and the end. So you might want to
rearrange things:

for s in students:
grades = []
grades.append(s.school)
grades.append(s.name)
grades.append(s.finalGrade())
if s.finalGrade()>82:
grades.append("Pass")
else:
grades.append("Fail")
print(grades)

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
On 15/11/2023 20.25, Grizzy Adams via Python-list wrote:
> Hi & thanks for patience with what could be simple to you
>
> Have this (from an online "classes" tutorial)

There are lots of on-line classes!


> --- Start Code Snippit ---
>
> students = []
> grades = []
> for s in geographyClass:
> students.append(geographyStudent(s))
> for s in students:
> grades.append(s.school)
> grades.append(s.name)
> grades.append(s.finalGrade())
> if s.finalGrade()>82:
> grades.append("Pass")
> else:
> grades.append("Fail")
> print(grades)
>
> --- End Code Snippit ---
>
> I have extended (from tutorial) it a bit, I would really like to have a newline
>
> at end of each record, I have searched (and tested) but cant get "\n" to give a
>
> newline, I get "Mydata\n"
>
> Do I need to replace "append" with "print", or is there a way to get the
> newline in as I append to list?

Don't know how "Mydata..." results - where is it in the code.

What do you see when grades is printed?
Do you really want data-values all mashed together?

Yes, what changed after removal of all the .append()-s, and instead,
within the (second) for-loop print( school, name, ... ) was used?

Is it easier to go on from there?

--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
On 15/11/2023 07:25, Grizzy Adams via Python-list wrote:

> for s in students:
> grades.append(s.school)
> grades.append(s.name)
> grades.append(s.finalGrade())
> if s.finalGrade()>82:
> grades.append("Pass")
> else:
> grades.append("Fail")
> print(grades)
>
> --- End Code Snippit ---

> Do I need to replace "append" with "print", or is there a way to get the
> newline in as I append to list?

Firstly, it is usually a bad idea to mix formatting features(like
newline) with the data. You will need to remove them again if you want
to work with the data itself.

So, better to print the raw data and add the formatting during printing.

There are a couple of options here (well more than a couple actually!)
The simplest is to create another for loop and print each field with a
newline automatically added by print()

Another is to simply join everything in grades together separated by
newlines. Python has a method to do that called join():

print('\n'.join(grades))

Unfortunately it seems your data has a mix of strings and numbers so
that won't work without some tweaks:

print('\n'.join(str(f) for f in grades))


However, I wonder if this really what you want? You have created grades
as a long list containing all of the attributes of all of the students
plus their Pass/Fail status. But you have no (easy)way to access the
Pass/Fail value for each student. Do you really want to store the
Pass/Fail in the student? And then print the students? Like so:

for s in students
if s.finalGrade() > 82:
s.result = "Pass"
else:
s.result = "Fail"
print(s.school)
print(s.name)
...
print(s.result)

Just a thought...

PS. There are neater ways to do this but you may not have covered
those yet so I'll stick to basics.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
Wednesday, November 15, 2023 at 9:50, Alan Gauld via Python-list wrote:
Re: Newline (NuBe Question) (at least in part)

>On 15/11/2023 07:25, Grizzy Adams via Python-list wrote:

>> for s in students:
>> grades.append(s.school)
>> grades.append(s.name)
>> grades.append(s.finalGrade())
>> if s.finalGrade()>82:
>> grades.append("Pass")
>> else:
>> grades.append("Fail")
>> print(grades)
>>
>> --- End Code Snippit ---

>> Do I need to replace "append" with "print", or is there a way to get the
>> newline in as I append to list?

>Firstly, it is usually a bad idea to mix formatting features(like
>newline) with the data. You will need to remove them again if you want
>to work with the data itself.

True, I onlt went that way when my (vain) attempts to add a newline any other
way, my usual language is VB/VBA, Delphi or C++ at a push, so I'm really a nube
here

>So, better to print the raw data and add the formatting during printing.

>There are a couple of options here (well more than a couple actually!)
>The simplest is to create another for loop and print each field with a
>newline automatically added by print()

>Another is to simply join everything in grades together separated by
>newlines. Python has a method to do that called join():

>print('\n'.join(grades))

>Unfortunately it seems your data has a mix of strings and numbers so
>that won't work without some tweaks:

>print('\n'.join(str(f) for f in grades))

that gets closer (sort of) my old code gave on long (only 5 records so far)
list,

[.'Example High', 'Mary', 89.6, 'Pass', 'Example High', 'Matthew', 76.5, 'Fail',
'Example High', 'Marie', 80.4, 'Fail', 'Example High', 'Manuel', 79.6, 'Fail',
'Example High', 'Malala', 98.9, 'Pass']

your code gives one tall column,

Example High
Mary
89.6
Pass
Example High
Matthew
76.5
Fail
Example High
Marie
80.4
Fail
Example High
Manuel
79.6
Fail
Example High
Malala
98.9
Pass

my ideal one row for each student (this I have edited manually)

Example High, Mary, 89.6, Pass
Example High, Matthew, 76.5, Fail
Example High, Marie, 80.4, Fail
Example High, Manuel, 79.6, Fail
Example High, Malala, 98.9, Pass

>However, I wonder if this really what you want? You have created grades as a
>long list containing all of the attributes of all of the students plus their
>Pass/Fail status. But you have no (easy)way to access the Pass/Fail value for
>each student. Do you really want to store the Pass/Fail in the student? And
>then print the students? Like so:

I do want to keep the data in tact, incase it gets reused later in the
tutorial(s)

>Just a thought...

>PS. There are neater ways to do this but you may not have covered
>those yet so I'll stick to basics.

I already jumped forward a bit (to python Classes)

Thanks
--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
On 11/15/2023 2:25 AM, Grizzy Adams via Python-list wrote:
> Hi & thanks for patience with what could be simple to you
>
> Have this (from an online "classes" tutorial)
>
> --- Start Code Snippit ---
>
> students = []
> grades = []
> for s in geographyClass:
> students.append(geographyStudent(s))
> for s in students:
> grades.append(s.school)
> grades.append(s.name)
> grades.append(s.finalGrade())
> if s.finalGrade()>82:
> grades.append("Pass")
> else:
> grades.append("Fail")
> print(grades)
>
> --- End Code Snippit ---
>
> I have extended (from tutorial) it a bit, I would really like to have a newline
>
> at end of each record, I have searched (and tested) but cant get "\n" to give a
>
> newline, I get "Mydata\n"
>
> Do I need to replace "append" with "print", or is there a way to get the
> newline in as I append to list?

First of all, if this is an accurate representation of the course
material, you need a better course. There's no sense in appending all
those values one after another in a single list since later it will be
very inconvenient to detect the end of one student's info and the start
of the next one's. And if you don't need to know that, but just want to
print out the data, you don't need to a list at all, just print it out
in the loop.

A list that contains lists of each student's data, one per interior
list, would make more sense.

Second, it is usual to append data to a list without print formatting,
and then add your formatting when you go to print the list. That way
you can use the list for other things beyond just printing, and the code
is clearer and simpler as well.

You may see responses that suggest various code alternatives. But you
haven't shown us an example of what you want the output to look like,
and you haven't said what else you plan to use the list for. So anyone
who responds has to fly blind, without knowing key information.

Asking for help is like writing code, with an added social element. You
have to be clear about the requirements, inputs, and desired outputs,
and you have to organize your request in a way that's easy for others to
understand and be willing to help. Your original post is partway there
already.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
Wednesday, November 15, 2023 at 9:45, Thomas Passin via Python-list wrote:
Re: Newline (NuBe Question) (at least in part)

>On 11/15/2023 2:25 AM, Grizzy Adams via Python-list wrote:
>> Hi & thanks for patience with what could be simple to you

>You may see responses that suggest various code alternatives. But you
>haven't shown us an example of what you want the output to look like,

Offered code got closer (sort of) my old code gave on long (only 5 records so
far) list,

[.'Example High', 'Mary', 89.6, 'Pass', 'Example High', 'Matthew', 76.5, 'Fail', 'Example High', 'Marie', 80.4, 'Fail', 'Example High', 'Manuel', 79.6, 'Fail', 'Example High', 'Malala', 98.9, 'Pass']

offered code gave one tall column,

Example High
Mary
89.6
Pass
Example High
Matthew
76.5
Fail
Example High
Marie
80.4
Fail
Example High
Manuel
79.6
Fail
Example High
Malala
98.9
Pass

my ideal is one row for each student (I had edited manually to show this)

Example High, Mary, 89.6, Pass
Example High, Matthew, 76.5, Fail
Example High, Marie, 80.4, Fail
Example High, Manuel, 79.6, Fail
Example High, Malala, 98.9, Pass

>and you haven't said what else you plan to use the list for. So anyone
>who responds has to fly blind, without knowing key information.

I'll keep list for a while in case it gets used or reused later, for now it's
just a test bed along with a few others, I can work thru as I learn

--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
On Wed, 15 Nov 2023 16:51:09 -0000 Grizzy Adams via Python-list wrote:

I don't give solutions; just a nudge... you appear not to fully grok
"list"; your list is ONE list with no delineation between students. You
want a "list of lists"...

>[.'Example High', 'Mary', 89.6, 'Pass', 'Example High', 'Matthew', 76.5, 'Fail', 'Example High', 'Marie', 80.4, 'Fail', 'Example High', 'Manuel', 79.6, 'Fail', 'Example High', 'Malala', 98.9, 'Pass']

Like this:

students = [
['Example High', 'Mary', 89.6, 'Pass'],
['Example High','Matthew', 76.5, 'Fail'],
['Example High', 'Marie', 80.4, 'Fail'],
['Example High', 'Manuel', 79.6, 'Fail'],
['Example High', 'Malala', 98.9, 'Pass']
]

This may help get you headed in the right direction:

for s in students:
print( s )

Hint: look forward to learning about f-strings...

HTH,
Pierre
--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
Wednesday, November 15, 2023 at 12:19, Pierre Fortin wrote:
Re: Newline (NuBe Question) (at least in part)

>On Wed, 15 Nov 2023 16:51:09 -0000 Grizzy Adams via Python-list wrote:
>
>I don't give solutions; just a nudge... you appear not to fully grok
>"list"; your list is ONE list with no delineation between students. You
>want a "list of lists"...

>>[.'Example High', 'Mary', 89.6, 'Pass', 'Example High', 'Matthew', 76.5, 'Fail', 'Example High', 'Marie', 80.4, 'Fail', 'Example High', 'Manuel', 79.6, 'Fail', 'Example High', 'Malala', 98.9, 'Pass']

>Like this:

>students = [
> ['Example High', 'Mary', 89.6, 'Pass'],
> ['Example High','Matthew', 76.5, 'Fail'],
> ['Example High', 'Marie', 80.4, 'Fail'],
> ['Example High', 'Manuel', 79.6, 'Fail'],
> ['Example High', 'Malala', 98.9, 'Pass']
>]

for now I made a copt of code and altered to

students = []
grades = []
for s in geographyClass:
students.append(geographyStudent(s))

for s in students:
if s.finalGrade()>82: Result=("Pass")
else: Result=("Fail")
print(s.school, s.name, s.finalGrade(),Result)

>This may help get you headed in the right direction:

>for s in students:
> print( s )

>Hint: look forward to learning about f-strings...

I will look forward to them, may even go search ahead,
--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
On 11/15/2023 2:04 PM, Grizzy Adams via Python-list wrote:
> Wednesday, November 15, 2023 at 12:19, Pierre Fortin wrote:
> Re: Newline (NuBe Question) (at least in part)
>
>> On Wed, 15 Nov 2023 16:51:09 -0000 Grizzy Adams via Python-list wrote:
>>
>> I don't give solutions; just a nudge... you appear not to fully grok
>> "list"; your list is ONE list with no delineation between students. You
>> want a "list of lists"...
>
>>> [.'Example High', 'Mary', 89.6, 'Pass', 'Example High', 'Matthew', 76.5, 'Fail', 'Example High', 'Marie', 80.4, 'Fail', 'Example High', 'Manuel', 79.6, 'Fail', 'Example High', 'Malala', 98.9, 'Pass']
>
>> Like this:
>
>> students = [
>> ['Example High', 'Mary', 89.6, 'Pass'],
>> ['Example High','Matthew', 76.5, 'Fail'],
>> ['Example High', 'Marie', 80.4, 'Fail'],
>> ['Example High', 'Manuel', 79.6, 'Fail'],
>> ['Example High', 'Malala', 98.9, 'Pass']
>> ]
>
> for now I made a copt of code and altered to
>
> students = []
> grades = []

# In this design there is no point in the extra loop.
# also, the indentation is wrong. Perhaps you inserted
# tabs? Use only spaces in these posts.
# Also you don't need the students list
> for student in geographyClass:
> # students.append(geographyStudent(s))
s = geographyStudent(student)
>
# for s in students:
> if s.finalGrade()>82: Result=("Pass")
> else: Result=("Fail")
> print(s.school, s.name, s.finalGrade(),Result)
>
>> This may help get you headed in the right direction:
>
>> for s in students:
>> print( s )
>
>> Hint: look forward to learning about f-strings...
>
> I will look forward to them, may even go search ahead,

--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
Wednesday, November 15, 2023 at 15:54, Thomas Passin via Python-list wrote:
Re: Newline (NuBe Question) (at least in part)

>On 11/15/2023 2:04 PM, Grizzy Adams via Python-list wrote:
>> Wednesday, November 15, 2023 at 12:19, Pierre Fortin wrote:
>> Re: Newline (NuBe Question) (at least in part)

>>> On Wed, 15 Nov 2023 16:51:09 -0000 Grizzy Adams via Python-list wrote:

>>> I don't give solutions; just a nudge... you appear not to fully grok
>>> "list"; your list is ONE list with no delineation between students. You
>>> want a "list of lists"...

>>>> [.'Example High', 'Mary', 89.6, 'Pass', 'Example High', 'Matthew', 76.5, 'Fail', 'Example High', 'Marie', 80.4, 'Fail', 'Example High', 'Manuel', 79.6, 'Fail', 'Example High', 'Malala', 98.9, 'Pass']

>>> Like this:

>>> students = [
>>> ['Example High', 'Mary', 89.6, 'Pass'],
>>> ['Example High','Matthew', 76.5, 'Fail'],
>>> ['Example High', 'Marie', 80.4, 'Fail'],
>>> ['Example High', 'Manuel', 79.6, 'Fail'],
>>> ['Example High', 'Malala', 98.9, 'Pass']
>>> ]

>> for now I made a copt of code and altered to
>>
>> students = []
>> grades = []

># In this design there is no point in the extra loop.
># also, the indentation is wrong. Perhaps you inserted
># tabs? Use only spaces in these posts.

I copy-pasted the code direct from IDLE, (to avoid any other typo's)

># Also you don't need the students list
>> for student in geographyClass:
>> # students.append(geographyStudent(s))
> s = geographyStudent(student)
>>
> # for s in students:
>> if s.finalGrade()>82: Result=("Pass")
>> else: Result=("Fail")
>> print(s.school, s.name, s.finalGrade(),Result)

I'll hive this a try (as a learning point)

Thanks
--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
On 11/16/2023 1:19 AM, Grizzy Adams via Python-list wrote:
> Wednesday, November 15, 2023 at 15:54, Thomas Passin via Python-list wrote:
> Re: Newline (NuBe Question) (at least in part)
>
>> On 11/15/2023 2:04 PM, Grizzy Adams via Python-list wrote:
>>> Wednesday, November 15, 2023 at 12:19, Pierre Fortin wrote:
>>> Re: Newline (NuBe Question) (at least in part)
>
>>>> On Wed, 15 Nov 2023 16:51:09 -0000 Grizzy Adams via Python-list wrote:
>
>>>> I don't give solutions; just a nudge... you appear not to fully grok
>>>> "list"; your list is ONE list with no delineation between students. You
>>>> want a "list of lists"...
>
>>>>> [.'Example High', 'Mary', 89.6, 'Pass', 'Example High', 'Matthew', 76.5, 'Fail', 'Example High', 'Marie', 80.4, 'Fail', 'Example High', 'Manuel', 79.6, 'Fail', 'Example High', 'Malala', 98.9, 'Pass']
>
>>>> Like this:
>
>>>> students = [
>>>> ['Example High', 'Mary', 89.6, 'Pass'],
>>>> ['Example High','Matthew', 76.5, 'Fail'],
>>>> ['Example High', 'Marie', 80.4, 'Fail'],
>>>> ['Example High', 'Manuel', 79.6, 'Fail'],
>>>> ['Example High', 'Malala', 98.9, 'Pass']
>>>> ]
>
>>> for now I made a copt of code and altered to
>>>
>>> students = []
>>> grades = []
>
>> # In this design there is no point in the extra loop.
>> # also, the indentation is wrong. Perhaps you inserted
>> # tabs? Use only spaces in these posts.
>
> I copy-pasted the code direct from IDLE, (to avoid any other typo's)
>
>> # Also you don't need the students list
>>> for student in geographyClass:
>>> # students.append(geographyStudent(s))
>> s = geographyStudent(student)
>>>
>> # for s in students:
>>> if s.finalGrade()>82: Result=("Pass")
>>> else: Result=("Fail")
>>> print(s.school, s.name, s.finalGrade(),Result)
>
> I'll hive this a try (as a learning point)

I wrote that you don't need the "students" list, which is correct. But
there could be a use for a list. It would let you change the order in
which students appear in the printed output. Knowing how to do that is
a useful skill. But that should be left for a later lesson, not mixed
in here.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
Thursday, November 16, 2023 at 7:47, Thomas Passin via Python-list wrote:
Re: Newline (NuBe Question) (at least in part)

>I wrote that you don't need the "students" list, which is correct. But
>there could be a use for a list. It would let you change the order in
>which students appear in the printed output. Knowing how to do that is
>a useful skill. But that should be left for a later lesson, not mixed
>in here.

I have a vague memory of seeing sorted list somewhere ;->)
--
https://mail.python.org/mailman/listinfo/python-list
RE: Newline (NuBe Question) [ In reply to ]
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]
]

magna = [stud for stud in studs if stud[2] >= 3.5 ]
summa = [stud for stud in studs if stud[2] >= 3.75 ]

print(studs, magna, summa, sep="\n")

OUTPUT:

>>> print(studs, magna, summa, sep="\n")
[['Peter', 82, 3.53], ['Paul', 77, 2.83], ['Mary', 103, 3.82]]
[['Peter', 82, 3.53], ['Mary', 103, 3.82]]
[['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 that are often
more efficient to represent your data such as some form of matrix or
data.frame.

And, yes, you can sort something like the above by name or GPA or number of
credits taken but the point was responding to why bother making a list just
to print it. The answer is that many and even most programs do a bit more
than that and a good choice of data structure facilitates ...




-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Grizzy Adams via Python-list
Sent: Thursday, November 16, 2023 8:41 AM
To: python-list@python.org
Subject: Re: Newline (NuBe Question)

Thursday, November 16, 2023 at 7:47, Thomas Passin via Python-list wrote:
Re: Newline (NuBe Question) (at least in part)

>I wrote that you don't need the "students" list, which is correct. But
>there could be a use for a list. It would let you change the order in
>which students appear in the printed output. Knowing how to do that is
>a useful skill. But that should be left for a later lesson, not mixed
>in here.

I have a vague memory of seeing sorted list somewhere ;->)
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: RE: Newline (NuBe Question) [ In reply to ]
On 24/11/2023 21.45, avi.e.gross@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]
> ]

I've seen Mary, and she didn't look like a "stud" to me.

> 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.


[1] <https://gist.github.com/hemanth/3715502>, as quoted by Bill
Lubanovic in _Introducing Python_

--
Michael F. Stemper
This sentence no verb.

--
https://mail.python.org/mailman/listinfo/python-list
Re: RE: Newline (NuBe Question) [ In reply to ]
On Sun, 26 Nov 2023 at 21:08, Michael F. Stemper via Python-list
<python-list@python.org> wrote:
>
> On 24/11/2023 21.45, avi.e.gross@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]
> > ]
>
> I've seen Mary, and she didn't look like a "stud" to me.
>

That's what happens when you abbreviate "student" though :) Don't
worry, there's far FAR worse around the place, and juvenile brains
will always find things to snigger at, usually in mathematical
libraries with "cumulative" functions.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
Michael F. Stemper via Python-list schreef op 25/11/2023 om 15:32:
> On 24/11/2023 21.45,avi.e.gross@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
--
https://mail.python.org/mailman/listinfo/python-list
RE: RE: Newline (NuBe Question) [ In reply to ]
That is an entirely different discussion, Michael.

I do not know what ideas Guido had ages ago and where he might stand now and
I actually seriously disagree with the snippet you quoted below.

Python was started long ago as a way to improve in some ways on what was
there before. Some of the ideas were nice but also for some purposes, way
too slow.

If you regard the original versions of LISP, they too simplicity to an
extreme and pretty much the main or even only data structure was a list.
Functions like CAR and CDR accessed an element but a complex structure
resulted in people creating functions with names like CAAAAAR and CADADADR
to automate climbing a tree of sorts to get to the parts you want. It was a
recipe for complexity and errors.

My point was that although a list can do so many things in principle, it is
not really optimized to do some things that can be way easier using add-ons
or your own data structures like objects and he notes the collection library
and deque as an example that he is not as much of a purist as you may think.

My point was that if you have a fairly detailed and complex application that
will manipulate lots of data, then instead of reading in a CSV with many
columns and rows recorded into a list of lists, it may make sense to import
numpy and pandas that come with all kinds of functionality built in so you
do not need to re-invent everything. Just how easy is it using lists of
lists to rearrange the order of columns of data, or add new columns
containing calculations built from existing columns and so on?

Of course, for many purposes, it is indeed overkill albeit once you learn a
method, ...

I think part of the design of Python, and this is just my guess, included
going away from overly specific things done in earlier compiled languages
and making it more abstract and inclusive. Arrays or vectors or other such
names would normally require everything to be of the same data type and with
a fixed length. The list data structure loosened this up quite a bit and
also allowed lists within lists. That is great and for some purposes, not
very efficient and especially not when your data actually is all of the same
type or of fixed length. You can make matrix-like data structures of any
depth using lists and it may be hard to traverse such as when you want to
multiply two such 2-D matrices. Place the same data (all say floating point
numbers) in a vector-like structure that also has stored info about the
dimensions, and a simple mathematical calculation accesses any item such as
may_tricks[5,42] in the same amount of time as an offset from the top.

I have seen this phenomenon in many languages where a somewhat clean and
sparse design gets added to, often by others, until some core features are
used less often. An example would be R which does have lists nut they are
just one form of vectors which are really more the core data idea. It also
contains data.frames in the core which are implemented as a list of vectors
and more recently a bit more. It was designed to do statistical tasks as one
of the main objectives. Yet the graphics functions have been added to so
there are by now quite a few independent ways to make graphics using
different paradigms. Python also has something like that. And completely new
paradigms such as piping data in a chain were added in packages and it
became so popular that a version has been added to the core language.

Now although the core language includes lots of the functionality you might
see in numpy/pandas and you can do all kinds of things, some others kept
creating new ways to do things including different data structures that
either dealt with weaknesses found or were ore efficient and so on and an
entire growing body of alternate ways to do things with lots more power and
often more speed that I prefer. A collection of lots of these alternative
packages has been assembled and I and others often simply start programs by
including the "tidyverse" and the resulting programs might as well be
written in a different language to anyone who only knows base R.

But I do not see that as a bad thing albeit someone trying to get a part of
a program from an AI-like service may need to specify or they may get code
they cannot trivially read and evaluate but that works fine once they have
loaded the packages.

Guido is like many others who create or invent and do it in the way they are
proud of. Why change it? But the reality is that first attempts are often
done with lots of room for change and improvement. Now if you are teaching a
course on Python basics, it may be a good idea to teach the basics and
require students to only use in their homework what has already been taught.
But if you get a job where the norm is to use modules like numpy, it makes
sense to use the expanded language if it results in faster writing perhaps
of faster code with fewer mistakes.
-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Michael F. Stemper via Python-list
Sent: Saturday, November 25, 2023 9:32 AM
To: python-list@python.org
Subject: Re: RE: Newline (NuBe Question)

On 24/11/2023 21.45, avi.e.gross@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]
> ]

I've seen Mary, and she didn't look like a "stud" to me.

> 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.


[1] <https://gist.github.com/hemanth/3715502>, as quoted by Bill
Lubanovic in _Introducing Python_

--
Michael F. Stemper
This sentence no verb.

--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
RE: RE: Newline (NuBe Question) [ In reply to ]
Just FYI, I deliberately chose that abbreviation for a sort of irony as for
some people college is about almost anything except learning and some people
think they are studs and just party and ...

And I am very tired of gender discussions. Lots of words now include two or
even more genders. Women are often now "actors", not actresses. I see no
reason women cannot be studs!

But I learn from criticism. If I ever write a program like that and do not
feel like typing, will this do?

dents = [ ...]

Or will that not include students who happen to be edentulous?


-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Chris Angelico via Python-list
Sent: Sunday, November 26, 2023 6:49 AM
To: python-list@python.org
Subject: Re: RE: Newline (NuBe Question)

On Sun, 26 Nov 2023 at 21:08, Michael F. Stemper via Python-list
<python-list@python.org> wrote:
>
> On 24/11/2023 21.45, avi.e.gross@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]
> > ]
>
> I've seen Mary, and she didn't look like a "stud" to me.
>

That's what happens when you abbreviate "student" though :) Don't
worry, there's far FAR worse around the place, and juvenile brains
will always find things to snigger at, usually in mathematical
libraries with "cumulative" functions.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
On 2023-11-25 08:32:24 -0600, Michael F. Stemper via Python-list wrote:
> On 24/11/2023 21.45, avi.e.gross@gmail.com wrote:
> > 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.
^^^^^^^^^^^^^^^

The key point here is *over*engineering. Don't make things more
complicated than they need to be. But also don't make them simpler than
necessary.

> Tuples are better than objects (try namedtuple too though).

If Guido thought that tuples would always be better than objects, then
Python wouldn't have objects. Why would he add such a complicated
feature to the language if he thought it was useless?

The (unspoken?) context here is "if tuples are sufficient, then ..."

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
Re: Newline (NuBe Question) [ In reply to ]
On 11/27/2023 12:48 AM, Chris Angelico via Python-list wrote:
> On Sun, 26 Nov 2023 at 21:08, Michael F. Stemper via Python-list
> <python-list@python.org> wrote:
>>
>> On 24/11/2023 21.45, avi.e.gross@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]
>>> ]
>>
>> I've seen Mary, and she didn't look like a "stud" to me.
>>
>
> That's what happens when you abbreviate "student" though :) Don't
> worry, there's far FAR worse around the place, and juvenile brains
> will always find things to snigger at, usually in mathematical
> libraries with "cumulative" functions.

The OP used an abbreviation: "studs". Why? Too lazy to type the full
word? Abbreviation has full-meaning in the (narrow) domain? Was wanting
something funny, or to snigger over?

Was the respondent sniggering? Perhaps he, like the OP, was also saving
typing-time by making a joke, hoping that the OP would see the
implicit-error in expecting others to understand that "studs" meant
"students"?

Actually, Peter, Paul, and Mary were a band
(https://www.peterpaulandmary.com/), so "studs" is even less expressive
when the data also tells a story...

Working with "trainees", I avoid the word "student" even though some
might see them as synonyms. In my mind, the abbreviation did not readily
expand to the full word (mea culpa).

Accordingly, would not pass Code Review!
For the want of a few characters...
(https://en.wikipedia.org/wiki/For_Want_of_a_Nail)

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list
Re: RE: Newline (NuBe Question) [ In reply to ]
On Mon, 27 Nov 2023 at 06:15, <avi.e.gross@gmail.com> wrote:
> But I learn from criticism. If I ever write a program like that and do not
> feel like typing, will this do?
>
> dents = [ ...]
>
> Or will that not include students who happen to be edentulous?
>

If they're learning to drive, this variable name would make complete sense.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
On 11/27/2023 1:08 AM, Roel Schroeven via Python-list wrote:
> 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.

+1
readability/comprehension!

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question) [ In reply to ]
On 11/27/2023 10:04 AM, Peter J. Holzer via Python-list wrote:
> On 2023-11-25 08:32:24 -0600, Michael F. Stemper via Python-list wrote:
>> On 24/11/2023 21.45, avi.e.gross@gmail.com wrote:
>>> 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.
> ^^^^^^^^^^^^^^^
>
> The key point here is *over*engineering. Don't make things more
> complicated than they need to be. But also don't make them simpler than
> necessary.
>
>> Tuples are better than objects (try namedtuple too though).
>
> If Guido thought that tuples would always be better than objects, then
> Python wouldn't have objects. Why would he add such a complicated
> feature to the language if he thought it was useless?
>
> The (unspoken?) context here is "if tuples are sufficient, then ..."


At recent PUG-meetings I've listened to a colleague asking questions and
conducting research on Python data-structures*, eg lists-of-lists cf
lists-of-tuples, etc, etc. The "etc, etc" goes on for some time!
Respecting the effort, even as it becomes boringly-detailed, am
encouraging him to publish his findings.

* sadly, he is resistant to OOP and included only a cursory look at
custom-objects, and early in the process. His 'new thinking' has been to
look at in-core databases and the speed-ups SQL (or other) might offer...

However, his motivation came from a particular application, and to
create a naming-system so that he could distinguish a list-of-lists
structure from some other tabular abstraction. The latter enables the
code to change data-format to speed the next process, without the coder
losing-track of the data-type/format.

The trouble is, whereas the research reveals which is faster
(in-isolation, and (only) on his 'platform'), my suspicion is that he
loses all gains by reformatting the data between 'the most efficient'
structure for each step. A problem of only looking at the 'micro',
whilst ignoring wider/macro concerns.

Accordingly, as to the word "engineering" (above), a reminder that we
work in two domains: code and data. The short 'toy examples' in training
courses discourage us from a design-stage for the former - until we
enter 'the real world' and meet a problem/solution too large to fit in a
single human-brain. Sadly, too many of us are pre-disposed to be
math/algorithmically-oriented, and thus data-design is rarely-considered
(in the macro!). Yet, here we are...

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list
RE: Newline (NuBe Question) [ In reply to ]
Isn't it fascinating that a meaningless piece of code used to illustrate
something can be analyzed as if it was full of malicious content?

Yes, my choice of names was as expected. The numbers chosen had no special
meaning other than choosing one number in each of three equivalence classes.

But, if you want me to add subtle meaning for generations to examine as it
it were a literary work, I offer this:

Peter and Paul were studs who got Mary'd.

Can we now go back to our regularly scheduled talking about aspects of a
computer language?

P.S.
And just for history, Paul was really Noel Paul Stookey but Peter, Paul &
Mary sounded more like new testament characters and I think Noel signifies a
birth to Peter and Mary, sort of, which might have fit too unless it was a
computer program where a name with an umlaut was once not common. Another
interpretation is that Noel came from the Latin word for news. Be that as it
may, and I have no interest in this topic, in the future I may use the ever
popular names of Primus, Secundus and Tertius and get blamed for using
Latin.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of DL Neil via Python-list
Sent: Sunday, November 26, 2023 4:58 PM
To: python-list@python.org
Subject: Re: Newline (NuBe Question)

On 11/27/2023 12:48 AM, Chris Angelico via Python-list wrote:
> On Sun, 26 Nov 2023 at 21:08, Michael F. Stemper via Python-list
> <python-list@python.org> wrote:
>>
>> On 24/11/2023 21.45, avi.e.gross@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]
>>> ]
>>
>> I've seen Mary, and she didn't look like a "stud" to me.
>>
>
> That's what happens when you abbreviate "student" though :) Don't
> worry, there's far FAR worse around the place, and juvenile brains
> will always find things to snigger at, usually in mathematical
> libraries with "cumulative" functions.

The OP used an abbreviation: "studs". Why? Too lazy to type the full
word? Abbreviation has full-meaning in the (narrow) domain? Was wanting
something funny, or to snigger over?

Was the respondent sniggering? Perhaps he, like the OP, was also saving
typing-time by making a joke, hoping that the OP would see the
implicit-error in expecting others to understand that "studs" meant
"students"?

Actually, Peter, Paul, and Mary were a band
(https://www.peterpaulandmary.com/), so "studs" is even less expressive
when the data also tells a story...

Working with "trainees", I avoid the word "student" even though some
might see them as synonyms. In my mind, the abbreviation did not readily
expand to the full word (mea culpa).

Accordingly, would not pass Code Review!
For the want of a few characters...
(https://en.wikipedia.org/wiki/For_Want_of_a_Nail)

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list

1 2  View All