我在通過for循環迭代之後努力對齊一些數據。我想每個for循環輸出一個單獨的列,但未能弄清楚如何完成這一點。我試過用end =''結尾來換行,但並沒有把下一列帶回頂端。你能幫忙嗎?下面是我試圖構建的代碼的可測試示例。非常感謝您的幫助。Python並列打印列
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()
電流輸出:
==========================================================================================
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
所需的輸出:
=============================================================================================
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
燦在打印之前,是否將所有數據最初存儲在'lists'中,然後將它們壓縮(或一起迭代)? –
只要'從__future__導入print_function'並連續執行'print(「...」,end =「」)'不應該破壞行。 [如何在沒有換行符或空格的Python中打印?](http://stackoverflow.com/questions/493386/how-to-print-in-python-without-newline-or-space) – mhoff
@MichaelHoff,我試過了但它只是打印一行中的所有行而不是列。如果它已經爲你工作,你可以請發佈修改後的代碼? – MBasith