【发布时间】:2016-11-16 19:00:07
【问题描述】:
以可测试代码为例重新发布。
大家好。各位大侠能帮我解答一下吗?我一直在尝试获取 for 循环的输出并使用它来打印摘要页面。我希望 for 循环中的每次迭代都成为最后一次迭代旁边的一列。你能帮助实现这一目标吗?非常感谢您的帮助。
导入时间、re、集合、运算符
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']]
def count_types():
item_1 = mm_counts(1)
item_2 = mm_counts(4)
item_3 = mm_counts(3)
def mm_counts(a):
r = []
for i in output_list:
x = (i[a])
#x = (i[0] + ': ' + i[a])
r.append(x)
y = collections.Counter(r)
#test_list = []
for k, v in sorted(y.items(), key=operator.itemgetter(1), reverse=True):
z = (str(k).ljust(5, ' ') + ' ' + (str(v).ljust(5, ' ')))
print(z) #<--- I want to print this column and iterate next columns next to each other.
count_types()
电流输出:
Magazine 1
Book 1
1234567 2
Podcast 2
期望的输出:
Magazine 1 1234567 2
Book 1 Podcast 2
【问题讨论】:
-
看起来您正在迭代一个空的
output_list开始。您能否发布一个可重现的示例,也许带有一些示例数据? -
如果我可以运行您的代码,我会尽力提供帮助。如果您正在寻找网格,您是否考虑过使用 pandas 之类的东西?
-
嗨,我重新发布了一个可测试的示例。您能否回顾一下,看看您是否可以为我指明正确的方向?