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?
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?
Have this (from an online "classes" tutorial)
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?
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:
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.
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?
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,
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.
['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']
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...
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 student in geographyClass:s = geographyStudent(student)
# students.append(geographyStudent(s))
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,
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:s = geographyStudent(student)
# 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)
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:s = geographyStudent(student)
# 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)
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.
[['Peter', 82, 3.53], ['Paul', 77, 2.83], ['Mary', 103, 3.82]]print(studs, magna, summa, sep="\n")
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.
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 [...]
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.
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.
Grizz[l]y,examples
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
such as having a list of lists containing a record where the third item isa
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 [...]
On 24/11/2023 21.45, avi.e.gross@gmail.com wrote:program
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
examplescan be useful beyond printing things once. There are many possible
is asuch as having a list of lists containing a record where the third item
aGPA for the student and writing a little list comprehension that selects
Cumsmaller list containing only students who are Magna Cum Laude or Summa
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.
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.
Tuples are better than objects (try namedtuple too though).
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?
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.
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.
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 ..."
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.
On Sun, 26 Nov 2023 at 21:08, Michael F. Stemper via Python-list <python-list@python.org> wrote:program
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
examplescan be useful beyond printing things once. There are many possible
is asuch as having a list of lists containing a record where the third item
aGPA for the student and writing a little list comprehension that selects
Cumsmaller list containing only students who are Magna Cum Laude or Summa
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.
On 2023-11-25 08:32:24 -0600, Michael F. Stemper via Python-list wrote:a
On 24/11/2023 21.45, avi.e.gross@gmail.com wrote:
Of course, for serious work, some might suggest avoiding constructs like
^^^^^^^^^^^^^^^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 ..."
Dave,
Back on a hopefully more serious note, I want to make a bit of an analogy with what happens when you save data in a format like a .CSV file.
Often you have a choice of including a header line giving names to the resulting columns, or not.
If you read in the data to some structure, often to some variation I would loosely call a data.frame or perhaps something like a matrix, then without headers you have to specify what you want positionally or create your own names for columns to use. If names are already there, your program can manipulate things by using the names and if they are well chosen, with no studs among them, the resulting code can be quite readable. More
importantly, if the data being read changes and includes additional columns or in a different order, your original program may run fine as long as the names of the columns you care about remain the same.
Positional programs can be positioned to fail in quite subtle ways if the positions no longer apply.
As I see it, many situations where some aspects are variable are not ideal for naming. A dictionary is an example that is useful when you have no idea how many items with unknown keys may be present. You can iterate over the names that are there, or use techniques that detect and deal with keys from your list that are not present. Not using names/keys here might involve a longer list with lots of empty slots to designate missing items, This
clearly is not great when the data present is sparse or when the number of items is not known in advance or cannot be maintained in the right order.
There are many other situations with assorted tradeoffs and to insist on using lists/tuples exclusively would be silly but at the same time, if you are using a list to hold the real and imaginary parts of a complex number,
or the X/Y[/Z] coordinates of a point where the order is almost universally accepted, then maybe it is not worth using a data structure more complex or derived as the use may be obvious.
I do recall odd methods sometimes used way back when I programmed in C/C++
or similar languages when some method was used to declare small constants like:
#define FIRSTNAME 1
#define LASTNAME 2
Or concepts like "const GPA = 3"
And so on, so code asking for student_record[LASTNAME] would be a tad more readable and if the order of entries somehow were different, just redefine the constant.
In some sense, some of the data structures we are discussing, under the
hood, actually may do something very similar as they remap the name to a small integer offset. Others may do much more or be slower but often add value in other ways. A full-blown class may not just encapsulate the names
of components of an object but verify the validity of the contents or do logging or any number of other things. Using a list or tuple does nothing else.
So if you need nothing else, they are often suitable and sometimes even preferable.
Dave,
Back on a hopefully more serious note, I want to make a bit of an analogy with what happens when you save data in a format like a .CSV file.
Often you have a choice of including a header line giving names to the resulting columns, or not.
If you read in the data to some structure, often to some variation I would loosely call a data.frame or perhaps something like a matrix, then without headers you have to specify what you want positionally or create your own names for columns to use. If names are already there, your program can manipulate things by using the names and if they are well chosen, with no studs among them, the resulting code can be quite readable. More
importantly, if the data being read changes and includes additional columns or in a different order, your original program may run fine as long as the names of the columns you care about remain the same.
Positional programs can be positioned to fail in quite subtle ways if the positions no longer apply.
As I see it, many situations where some aspects are variable are not ideal for naming. A dictionary is an example that is useful when you have no idea how many items with unknown keys may be present. You can iterate over the names that are there, or use techniques that detect and deal with keys from your list that are not present. Not using names/keys here might involve a longer list with lots of empty slots to designate missing items, This
clearly is not great when the data present is sparse or when the number of items is not known in advance or cannot be maintained in the right order.
There are many other situations with assorted tradeoffs and to insist on using lists/tuples exclusively would be silly but at the same time, if you are using a list to hold the real and imaginary parts of a complex number,
or the X/Y[/Z] coordinates of a point where the order is almost universally accepted, then maybe it is not worth using a data structure more complex or derived as the use may be obvious.
I do recall odd methods sometimes used way back when I programmed in C/C++
or similar languages when some method was used to declare small constants like:
#define FIRSTNAME 1
#define LASTNAME 2
Or concepts like "const GPA = 3"
And so on, so code asking for student_record[LASTNAME] would be a tad more readable and if the order of entries somehow were different, just redefine the constant.
In some sense, some of the data structures we are discussing, under the
hood, actually may do something very similar as they remap the name to a small integer offset. Others may do much more or be slower but often add value in other ways. A full-blown class may not just encapsulate the names
of components of an object but verify the validity of the contents or do logging or any number of other things. Using a list or tuple does nothing else.
So if you need nothing else, they are often suitable and sometimes even preferable.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 61:16:18 |
Calls: | 6,712 |
Files: | 12,244 |
Messages: | 5,355,810 |