Reputation: 4190
I have a list of tuples, called gradebook
, where each list element is a tuple that corresponds to a class and a grade that a student can earn. For example,
gradebook = [('Math 212', 'Linear Algebra', 'Fall 2012', 'B'),
('CS 130', 'Python', 'Spring 2013', 'A')]
And so on. I would like it to print like this:
Class: Math 212.....Subject: Linear Algebra.....Term: Fall 2012.....Grade: B`
Class: CS 130.......Subject: Computer Science...Term: Spring 2013...Grade: A`
I would like to be able to go through each tuple in the list, and then print out each element of the tuple. How can I achieve this?
EDIT: This is what I have right now:
for aTuple in gradebook:
print(aTuple)
Sorry, I'm very new to Python, so I don't really understand how this works.
Upvotes: 19
Views: 93450
Reputation: 27585
gradebook = [('Math 212', 'Linear Algebra', 'Fall 2012', 'B'),
('CS 130', 'Python', 'Spring 2013', 'A'),
('Economics History','1914','Fall 14','D')]
fields = '...'.join( '{:.<%ds}' % max(map(len,cat))
for cat in zip(*gradebook) )
print 'fields :\n%r\n\n' % fields
def disp(x,fields=fields):
if all(isinstance(el,tuple) for el in x):
# x is a collections of tuples
print '\n'.join(fields.format(*el) for el in x)
elif all(isinstance(el,str) for el in x):
# x is a collection of strings
print fields.format(*x)
print 'disp(gradebook) :\n\n',
disp(gradebook)
print '\n'
print 'disp(gradebook[1]) :\n\n',
disp(gradebook[1])
result
fields :
'{:.<17s}...{:.<14s}...{:.<11s}...{:.<1s}'
disp(gradebook) :
Math 212............Linear Algebra...Fall 2012.....B
CS 130..............Python...........Spring 2013...A
Economics History...1914.............Fall 14.......D
disp(gradebook[1]) :
CS 130..............Python...........Spring 2013...A
Upvotes: 1
Reputation: 11
you can define a function naming __str__(self)
, which return a string like the form "Class: Math 212.....Subject: Linear Algebra.....Term: Fall 2012.....Grade: B" in the class .
then you can use your code:
for aTuple in gradebook:
print(aTuple)
to get the expected output.
Upvotes: 1
Reputation: 831
I'm not too familiar with some of the more advanced formatting options in python. That being said, this will display the results as requested. You can access the elements in each tuple by their index. '.'*(#-len('column info'+g[i]))
gives the correct number of periods by subtracting the length of the string from the column width. To print this without spaces between elements you use the sep=''
in print()
gradebook = [('Math 212', 'Linear Algebra', 'Fall 2012', 'B'), ('CS 130', 'Python', 'Spring 2013', 'A')]
for g in gradebook:
print('Class: ', g[0], '.'*(20-len('Class: '+g[0])),
'Subject: ', g[1], '.'*(28-len('Subject: '+g[1])),
'Term: ', g[2], '.'*(20-len('Term: '+g[2])),
'Grade: ', g[3], sep = '')
Upvotes: 0
Reputation: 1601
General format, you can iterate through a list and access the index of a tuple:
for x in gradebook:
print x[0], x[1]
x[0] in this example will give you the first part of the tuple, and x[1] .... so on. Mess around and experiment with that format, and you should be able to do the rest on your own.
EDIT: Although some of the other answers are nicer here, in the sense that they unpack the tuples and follow the "way of Python" more closely. Like such:
a, b, c = ('a','b','c')
Upvotes: 23
Reputation: 891
You can index by assigning elements names (sometimes convenient if you're calculating something):
for (a, b, c, d) in gradebook:
print "Class: ", a, "...Subject: ", b, "...Term: ", c, "...Grade: ", d
Class: Math 212 ...Subject: Linear Algebra ...Term: Fall 2012 ...Grade: B
Class: CS 130 ...Subject: Python ...Term: Spring 2013 ...Grade: A
For more even spacing:
for (a, b, c, d) in gradebook:
print "Class: ", a, "."*(20-len(a)), "Subject: ", b, "."*(20-len(b)), "Term: ", c, "."*(20-len(c)), "Grade: ", d
Class: Math 212 ............ Subject: Linear Algebra ...... Term: Fall 2012 ........... Grade: B
Class: CS 130 .............. Subject: Python .............. Term: Spring 2013 ......... Grade: A
Upvotes: 0
Reputation: 363556
gradebook = [('Math 212', 'Linear Algebra', 'Fall 2012', 'B'), ('CS 130', 'Python', 'Spring 2013', 'A')]
fieldwidths = 13, 19, 14, 1
for tup in gradebook:
tup = (s.ljust(w, '.') for s,w in zip(tup, fieldwidths))
print 'Class: {}Subject: {}Term: {}Grade: {}'.format(*tup)
I have manually set the field widths to match your example. But you might prefer to generate fieldwidths in a smart way, i.e. based on column-maximums of element lengths in gradebook.
Next time, a better data structure for your gradebook entries would be a dict
instead of a tuple
.
Upvotes: 4
Reputation: 6086
Use string formatting:
for aTuple in gradebook:
print('Class: %s.....Subject: %s.....Term: %s.....Grade: %s' % aTuple)
Upvotes: 0
Reputation: 2629
Or you could do this...
for id, name, semester, grade in gradebook:
print id, name, semester, grade
Upvotes: 10