I'm struggling to align some data after iterating it through a for loop. I'd like to have each for loop output a separate column but failing to figure out how to accomplish this. I've tried ending with end='' for new lines but does not bring the next column back top. Can you please help? Below is a testable example of the code I am trying to build. Your help is well appreciated.
import time, re, collections, operator
output_list = [['2016-07-12', 'Magazine', 'News Paper #2', 'Podcast', '1234567', '10-10-10-10', 'ABCDEFG', 'Zoo'],
['2016-07-12', 'Book', 'News Paper #2', 'Podcast', '1234567', '10-10-10-10', 'ABCDEFG', 'Zoo'],
['2016-07-13', 'Book', 'News Paper #2', 'Podcast', '1234567', '10-10-10-10', 'ABREF', 'Zoo'],
['2016-07-14', 'Article', 'News Paper #4', 'Radio', '1234567', '10-10-10-10', 'ABCDEFG', 'Zoo'],
['2016-07-15', 'Article', 'News Paper #4', 'Radio', '1234567', '10-10-10-10', 'ABCDEFG', 'Zoo'],
['2016-07-15', 'Snippet', 'News Paper #2', 'Podcast', '1234567', '10-10-10-10', 'ABCDEFG', 'Zoo']]
def count_types():
#item_1 = mm_counts(0)
item_2 = mm_counts(1)
item_3 = mm_counts(2)
item_4 = mm_counts(3)
item_5 = mm_counts(4)
item_6 = mm_counts(5)
item_7 = mm_counts(6)
item_8 = mm_counts(7)
print('=' * 90)
print(('Media1 Media2 Media3 Media4 Media5 Media6 Media7'))
print('=' * 90)
def mm_counts(a):
r = []
for i in output_list:
x = (i[a])
r.append(x)
y = collections.Counter(r)
padding = 9
for k, v in sorted(y.items(), key=operator.itemgetter(1), reverse=True):
z = (str(k).ljust(13, ' ') + ' ' + (str(v).ljust(5, ' ')))
print (z)
count_types()
Current Output:
==========================================================================================
Media1 Media2 Media3 Media4 Media5 Media6 Media7
==========================================================================================
Article 2
Book 2
Snippet 1
Magazine 1
News Paper #2 4
News Paper #4 2
Podcast 4
Radio 2
1234567 6
10-10-10-10 6
ABCDEFG 5
ABREF 1
Zoo 6
Desired Output:
=============================================================================================
Media1 Media2 Media3 Media4 Media5 Media6 Media7
==============================================================================================
Article 2 Magazine 1 Podcast 4 1234567 6 10-10-10-10 6 ABCDEFG 5 Zoo 6
Book 2 News Paper #2 4 Radio 2 ABREF 1
Snippet 1 News Paper #4 2
Magazine 1