我在通过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