So this is probably a very basic question about output formatting in python using '.format' and since I'm a beginner, I can't figure this out for the life of me. I've tried to be as detailed as possible, just to make sure that there's no confusion.
Let me give you an example so that you can better understand my dilemma. Consider the following program
list = (['wer', 'werwe', 'werwe' ,'wer we']) # list[0], list[1], list[2], list[3]
list.append(['vbcv', 'cvnc', 'bnfhn', 'mjyh']) # list[4]
list.append(['yth', 'rnhn', 'mjyu', 'mujym']) # list[5]
list.append(['cxzz', 'bncz', 'nhrt', 'qweq']) # list[6]
first = 'bill'
last = 'gates'
print ('{:10} {:10} {:10} {:10}'.format(first,last,list[5], list[6]))
Understandably that would give the output:
bill gates ['yth', 'rnhn', 'mjyu', 'mujym'] ['cxzz', 'bncz', 'nhrt', 'qweq']
So here's my real question. I was doing this practice problem from the book and I don't understand the answer. The program below will give you a good idea of what kind of output we are going for:
students = []
students.append(['DeMoines', 'Jim', 'Sophomore', 3.45]) #students[0]
students.append(['Pierre', 'Sophie', 'Sophomore', 4.0]) #students[1]
students.append(['Columbus', 'Maria', 'Senior', 2.5]) #students[2]
students.append(['Phoenix', 'River', 'Junior', 2.45]) #students[3]
students.append(['Olympis', 'Edgar', 'Junior', 3.99]) #students[4]
students.append(['van','john', 'junior', 3.56]) #students[5]
def Grades(students):
print ('Last First Standing GPA')
for students in students:
print('{0:10} {1:10} {2:10} {3:8.2f}'.format(students[0],students[1],students[2],students[3]))
The output we're trying to get is a kind of a table that gives all the stats for all the students -
Last First Standing GPA
DeMoines Jim Sophomore 3.45
Pierre Sophie Sophomore 4.00
Columbus Maria Senior 2.50
Phoenix River Junior 2.45
Olympis Edgar Junior 3.99
van john junior 3.56
So here's what I don't understand. We are working with basically the same thing in the two examples ie a list inside a list. For my first example, the print statement was:
print('{:10} {:10} {:10} {:10}'.format(first, last, list[5], list[6]))
where list[5]
and list[6]
are lists themselves and they are printed in entirety, as you can see from the output. But that doesn't happen in the book problem . There, the print statement says
print('{0:10} {1:10} {2:10} {3:8.2f}'.format(students[0], students[1], students[2], students[3]))
As you can see from the table output, here students[0]
refers only to 'DeMoines' . But if you just run the statement students[0]
in the Python interpreter, it gives the whole sub list, as it should.
['DeMoines', 'Jim', 'Sophomore', 3.45]
So, basically, I've got two questions, why does students[0]
have two different meanings and why does students[0]
not print the whole list like we did with list[5]
and list[6]
?
Look at the for loop :
for students in students:
# ^^^^^^^^
So, students
(inside loop) does not actually refers to list of list . And students[0]
refers to first element from element from list of lists , as expected.
I suggest replace students
from function argument, say, with all_students
or something like that.
Try renaming the variable list
into something that's not a reserved word or built-in function or type.
What's confusing to beginners - and it happens to everyone sooner or later - is what happens if you redefine or use in unintended ways a reserved word or a builtin.
If you do
list = [1, 2, 3, 4]
you re-bind the name list
to no longer point to the builtin list
data type but to the actual list [1, 2, 3, 4]
in the current scope. That is almost always not what you intend to do. Using a variable dir
is a similar pitfall.
Also do not use additional parantheses ()
around the square brackets of the list assignment. Something like words = ['wer', 'werwe', 'werwe' ,'wer we']
suffices.
Generally consider which names you choose for a variable. students
is descriptive, helpful commentary, list
is not. Also if list
currently holds a list, your algorithm might be changed later on with the variable holding a set
or any other container type. Then a type-based variable name will be even misleading.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.