Reputation: 1734
I am using Python 3.5.2, and I want to create an user-friendly program that outputs a range of numbers in some columns.
#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.")
Until that everything is fine, but here I'm stuck:
#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!")
I tried to use that code and it does what I want except because it involves to have hardcoded the number of columns. x,y,z for example would be for 3 columns, but I want to set the number of columns at the User input and remove the need to hardcode it everytime.
What am I missing? How can I make the print understand how many lists do I have?
Desired output: If user sets number of columns on 4, for example, the output would be:
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
Etc...
Upvotes: 1
Views: 112
Reputation: 20025
Use:
for t in zip(generated_lists[0],generated_lists[1],generated_lists[2]):
print(' '.join(str(x) for x in t))
or more succinctly:
for t in zip(*generated_lists[:3]):
print(' '.join(map(str, t)))
So what you need to change is 3 to whatever number you want
Upvotes: 2