以可測試的代碼重新發布爲例。Python從for循環獲取輸出並將其分配到其他地方
大家好。你們能和我們一起幫我解決這個問題嗎?我一直在嘗試使用for循環的輸出並使用它打印摘要頁面。我希望for循環中的每個迭代成爲最後一次迭代旁邊的列。你能幫助實現這一目標嗎?非常感謝您的幫助。
進口時間,重新,收藏,經營者
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'作爲開始。你可以發佈一個可重現的例子,也許有一些樣本數據? –
如果我可以運行你的代碼,我會盡力幫忙。 如果你正在尋找一個網格,你有沒有想過使用類似熊貓的東西? – GMarsh
嗨,我重新張貼了一個可測試的例子。你能否請回顧一下,看看你能否指引我朝着正確的方向發展? – MBasith