Home > Blockchain >  How do I put multiple outputs in several lists?
How do I put multiple outputs in several lists?

Time:06-13

I've got an assigment to do and I'd want to automate the process as much as possible. Basically I have to test how much time does algorithm need to perform merge sort and do so 10 times for 10 seeds, and then for 5 different ranges ((0,600), (0,700), ...).

So far I've managed to make the code spit out all numbers I need, but now I wonder how can I process them (I mean counting mean, max-min value and such) for each run.

Here's the code:

import time, random, statistics

myseed = 0
myrange = 600

randomlist = []
random.seed(myseed)

def generate_random_list():
    for i in range(0,myrange):
        randomlist.append(random.randint(0,10000))

generate_random_list()


def mergeSort(arr):
    if len(arr) > 1:
            
        mid = len(arr)//2
        L = arr[:mid]
        R = arr[mid:]
        mergeSort(L)
        mergeSort(R)

        i = j = k = 0

        while i < len(L) and j < len(R):
            if L[i] < R[j]:
                arr[k] = L[i]
                i  = 1
            else:
                arr[k] = R[j]
                j  = 1
            k  = 1

        while i < len(L):
            arr[k] = L[i]
            i  = 1
            k  = 1

        while j < len(R):
            arr[k] = R[j]
            j  = 1
            k  = 1

arr = randomlist
    
    
def procedure():
    starttime = time.time()
    mergeSort(arr)
    endtime = time.time()
    elapsedtime = endtime - starttime
    
    print(elapsedtime)


while myrange <= 1000:
    print(f"\n this is range {myrange}")
    
    while myseed <= 9:
        print(f"\n this is seed {myseed}")
        
        for x in range(0,10):
            procedure()
        
        myseed  = 1
                
    
    myrange  = 100
    myseed = 0

So far I've thought about putting outputs in several different lists, but how can I do that when I'm using the loop? I'd need it to change the name between iterations, and I have no idea how to do that.

Anyways, all of your ideas are welcome, now I'm getting back to trying to figure it out, cheers.

CodePudding user response:

you could collect your results in a dictionary

results = {}
def procedure(oi, ii):
    ....
    if not oi in results: results[oi] = {}
    results[oi][ii] = elapsedtime
...
procedure(int(myrange / 100 - 6), myseed)
...

resulting dictionary looks something like

{
0: {
  0: 0.00019431114196777344, 
  1: 0.0001773834228515625, 
  2: 0.00019407272338867188, 
  3: 0.00017881393432617188, 
  4: 0.00019359588623046875, 
  5: 0.00019812583923339844,
  6: 0.00019693374633789062, 
  7: 0.00019693374633789062, 
  8: 0.00019693374633789062, 
  9: 0.00019598007202148438
}, 
1: {
  0: 0.00019621849060058594, 
  1: 0.00017881393432617188, 
  2: 0.00019693374633789062, 
  3: 0.00019741058349609375, 
  4: 0.00019621849060058594, 
  5: 0.00019407272338867188, 
  6: 0.00019884109497070312, 
  7: 0.0001957416534423828, 
  8: 0.0001938343048095703, 
  9: 0.00019478797912597656
},
...

Though if you prefer lists you can use

def procedure(oi):
    ...
    if not oi in results: results[oi] = []
    results[oi].append(elapsedtime)
...
procedure(int(myrange / 100 - 6))
...

output would be something like

{
0: [0.00021266937255859375, 0.0001983642578125, 0.00019550323486328125, 0.00019621849060058594, 0.00019431114196777344, 0.00019550323486328125, 0.00020694732666015625, 0.000194549560546875, 0.000194549560546875, 0.00019502639770507812, 0.00020265579223632812, 0.00019502639770507812, 0.00019502639770507812, 0.00019550323486328125, 0.00019502639770507812, 0.0001964569091796875, 0.00019621849060058594, 0.0001957416534423828, 0.00018024444580078125, 0.0001766681671142578, 0.00019669532775878906, 0.00019478797912597656, 0.00019550323486328125, 0.00019478797912597656, 0.00019502639770507812, 0.00019669532775878906, 0.00019478797912597656, 0.0001952648162841797, 0.0001938343048095703, 0.00019502639770507812, 0.0001983642578125, 0.00019502639770507812, 0.00019550323486328125, 0.00019502639770507812, 0.0001952648162841797, 0.00019621849060058594, 0.00019359588623046875, 0.000194549560546875, 0.00019478797912597656, 0.00019311904907226562, 0.00019741058349609375, 0.00019431114196777344, 0.0001952648162841797, 0.000194549560546875, 0.00021076202392578125, 0.00027823448181152344, 0.0001773834228515625, 0.0001766681671142578, 0.00017690658569335938, 0.00017786026000976562, 0.00019812583923339844, 0.000194549560546875, 0.0001933574676513672, 0.00019431114196777344, 0.00019431114196777344, 0.00019598007202148438, 0.0001952648162841797, 0.00019359588623046875, 0.0001938343048095703, 0.00019288063049316406, 0.0001983642578125, 0.00019311904907226562, 0.00019311904907226562, 0.0001933574676513672, 0.00019478797912597656, 0.0001952648162841797, 0.00019359588623046875, 0.0001933574676513672, 0.0001926422119140625, 0.00019288063049316406, 0.00019812583923339844, 0.0001933574676513672, 0.00019311904907226562, 0.00019478797912597656, 0.00019478797912597656, 0.0001964569091796875, 0.000194549560546875, 0.000194549560546875, 0.00019478797912597656, 0.0001952648162841797, 0.00019884109497070312, 0.00019621849060058594, 0.0001952648162841797, 0.0001957416534423828, 0.00019502639770507812, 0.0001971721649169922, 0.0001952648162841797, 0.00019550323486328125, 0.0001957416534423828, 0.0001952648162841797, 0.0001983642578125, 0.00019478797912597656, 0.00019478797912597656, 0.00019502639770507812, 0.0001933574676513672, 0.00019407272338867188, 0.00019359588623046875, 0.00019407272338867188, 0.00019431114196777344, 0.00019407272338867188], 
1: [0.00019788742065429688, 0.000194549560546875, 0.00019431114196777344, 0.00019669532775878906, 0.00019478797912597656, 0.00019431114196777344, 0.000194549560546875, 0.00019502639770507812, 0.0002193450927734375, 0.00017690658569335938, 0.0001983642578125, 0.00019502639770507812, 0.00019502639770507812, 0.00019502639770507812, 0.000194549560546875, 0.0001964569091796875, 0.0001957416534423828, 0.0001957416534423828, 0.00019550323486328125, 0.00019478797912597656, 0.00019860267639160156, 0.00019598007202148438, 0.00019598007202148438, 0.00019550323486328125, 0.0001957416534423828, 0.0002803802490234375, 0.00017690658569335938, 0.0001766681671142578, 0.00017690658569335938, 0.0001761913299560547, 0.00021266937255859375, 0.0001964569091796875, 0.0001964569091796875, 0.0001957416534423828, 0.0001971721649169922, 0.0001952648162841797, 0.00019478797912597656, 0.00019502639770507812, 0.00019478797912597656, 0.00019478797912597656, 0.00019741058349609375, 0.0001957416534423828, 0.00019502639770507812, 0.00019478797912597656, 0.00019359588623046875, 0.00019669532775878906, 0.0001957416534423828, 0.00019407272338867188, 0.000194549560546875, 0.00019550323486328125, 0.0001983642578125, 0.00019478797912597656, 0.0001952648162841797, 0.000194549560546875, 0.00019478797912597656, 0.0001964569091796875, 0.00019550323486328125, 0.00019502639770507812, 0.00019478797912597656, 0.00019478797912597656, 0.00019741058349609375, 0.00019478797912597656, 0.00019478797912597656, 0.00019478797912597656, 0.00019502639770507812, 0.00019741058349609375, 0.00019550323486328125, 0.00019407272338867188, 0.0001952648162841797, 0.00019502639770507812, 0.0001976490020751953, 0.0001952648162841797, 0.00019478797912597656, 0.000194549560546875, 0.0001933574676513672, 0.0001957416534423828, 0.0001933574676513672, 0.0001933574676513672, 0.00019359588623046875, 0.00019359588623046875, 0.00019621849060058594, 0.00019288063049316406, 0.0001938343048095703, 0.0001938343048095703, 0.0001933574676513672, 0.0001957416534423828, 0.00019359588623046875, 0.0001938343048095703, 0.00019431114196777344, 0.00019407272338867188, 0.0001964569091796875, 0.00019502639770507812, 0.00019359588623046875, 0.0001952648162841797, 0.00019598007202148438, 0.00019240379333496094, 0.0001952648162841797, 0.0001938343048095703, 0.0001938343048095703, 0.000194549560546875], 
...
}

CodePudding user response:

First an observation: your code iterates correctly through different values of myrange and myseed, but it always sorts the same random list as it never creates a new one.

One option for a) fixing this and b) collecting the 10 results for the same (myrange, myseed) parameter combination would be:

while myrange <= 1000:
    print(f"\n this is range {myrange}")
    
    while myseed <= 9:
        print(f"\n this is seed {myseed}")
        
        randomlist = [] # so the random list does not grow all the time
        generate_random_list() # generate a new list for the next 10 measurements
        arr = randomlist # so procedure() calls mergeSort on the right list
        
        elapsed_times = []
        for x in range(0,10):
            elapsed_time = procedure()
            elapsed_times.append(elapsed_time)
        print(f"average: {sum(elapsed_times) / len(elapsed_times)}")
        
        myseed  = 1
                
    
    myrange  = 100
    myseed = 0

You'll notice that this requires that procedure() returns the elapsed time like this:

def procedure():
    starttime = time.time()
    mergeSort(arr)
    endtime = time.time()
    elapsedtime = endtime - starttime

    print(elapsedtime)
    return elapsedtime

This gets you:

...
 this is seed 9
0.0035524999998415296
0.0029768000003969064
0.004242299999987154
0.003511800000069343
0.0029614000000037777
0.003436700000293058
0.004654999999729625
0.009417000000212283
0.005804900000384805
0.0032578999998804647
average: 0.0043816300000798945

If you want to calculate the average over different seeds you can use another list in the outer while loop to collect the averages from the inner loop.

The code in the inner while loop is a little awkward because it manipulates the global variables arr and randomlist. It would be much easier to understand if it passed the random list as a parameter to procedure() which could pass it on to mergeSort(). (But I didn't want to change too much of your code.)

  • Related