1
我正在使用Python 3.5.2,我想創建一個用戶友好的程序,在某些列中輸出一系列數字。列出未知數量的列表作爲列
#User input
start = 0
until = 50
number_of_columns = 4
#Programmer
#create list of numbers
list_of_stuff = [str(x) for x in range(start,until)]
print("-Created "+str(len(list_of_stuff))+" numbers.")
#calculate the number of numbers per column
stuff_per_column = int(len(list_of_stuff)/number_of_columns)
print("-I must add "+str(stuff_per_column)+" numbers on each column.")
#generate different lists with their numbers
generated_lists = list(zip(*[iter(list_of_stuff)]*stuff_per_column))
print("-Columns are now filled with their numbers.")
在此之前,一切都很好,但這裏我堅持:
#print lists together as columns
for x,y,z in zip(generated_lists[0],generated_lists[1],generated_lists[2]):
print(x,y,z)
print("-Done!")
我想使用的代碼,它不會是我想要的東西,除了因爲它涉及到了硬編碼數列。例如,x,y,z將用於3列,但我想在用戶輸入中設置列數,並且不需要每次都對其進行硬編碼。
我錯過了什麼?我怎樣才能讓打印瞭解我有多少個列表?
希望的輸出: 如果用戶設定了4列的數目,例如,輸出將是:
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
Etc...
for zip in(* generated_lists [:number_of_columns])works。只要系統允許,我會盡快接受您的答案。感謝您的幫助:D – Saelyth
小問題:如果用戶選擇從0開始,直到20和2列,它只打印8個數字,而不是20個。任何修復方法? – Saelyth