2021年3月18日星期四

Using For Loops to create tabular input in Python from a list

My code is the following. There are 3 bus stops in the simulation and 10 iterations of these three stops. I also included the list of board rates and the number of iterations.

   def run_sim(iterations, board_rates):          mylist = []          numstops = len(board_rates)          for i in range(iterations):              stop = i + 1              mylist.append(stop)              for j in range(numstops):                  boarding = board_rates[j]                  mylist.append(boarding)              print('{0:^10d}{1:^10d}'.format(stop, boarding))          return mylist  

In this example, board_rates = [8, 10, 8] and iterations = 10

My output is:

1         8       1         10      1         8       2         8       2         10      2         8       3         8       3         10      3         8       4         8       4         10      4         8       5         8       5         10      5         8       6         8       6         10      6         8       7         8       7         10      7         8       8         8       8         10      8         8       9         8       9         10      9         8       10        8       10        10      10        8       

but I want my output to look like:

1         8         2         10       3         8       4         8       5         10       6         8       7         8        8         10       9         8       10        8       

What am I doing wrong?

https://stackoverflow.com/questions/66701160/using-for-loops-to-create-tabular-input-in-python-from-a-list March 19, 2021 at 08:54AM

没有评论:

发表评论