Advertisement
Guest User

Creating an Array and Sorting on It

a guest
May 14th, 2014
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # Name:        module2
  3. # Purpose:
  4. #
  5. # Author:      User
  6. #
  7. # Created:     12/05/2014
  8. # Copyright:   (c) User 2014
  9. # Licence:     <your licence>
  10. #-------------------------------------------------------------------------------
  11.     # Imports #
  12. import random
  13.  
  14.     # Initializations #
  15.  
  16.     # Global Variables #
  17.  
  18.     # Main #
  19. def main():
  20.     # Variables
  21.     v_NumInstances = 20
  22.     v_NumGroups = 5
  23.     v_StartGroup = 0
  24.  
  25.     o_Instance = [c_Instance(random.randint(0, 1000), random.randint(9999, 99999))\
  26.         for i in range(0,v_NumInstances)] ## Create some instances and pops with rand fitness, AS
  27.             ## 0 for Reprodnum info
  28.  
  29.     v_Array = [] ## Inits test v_Array
  30.     for i in range(0, v_NumInstances):
  31.         #print "Instance[%i]'s fitness is %i"\
  32.          #%(i, o_Instance[i].fitness)
  33.         v_Array.append([i,o_Instance[i].fitness, o_Instance[i].AS, o_Instance[i].ReprodClass]) ## Puts
  34.             ## each instance num and fitness and reprodclass in array
  35.     #print v_Array
  36.     v_Array.sort(key=f_SortKey) ## Sort fitness array on fitness number
  37.     #print "Sorted", v_Array
  38.  
  39.     print "Before"
  40.     for i in range(0, v_NumInstances):
  41.         print v_Array[i][0],v_Array[i][1], v_Array[i][2], v_Array[i][3]
  42.     print
  43.     print "After"
  44.  
  45.  
  46.  
  47.     v_TopGroup = (v_NumGroups - 1)## Setsup max group num
  48.     for i in range(0, v_NumGroups): ## Iters the groups
  49.         for j in range(v_StartGroup, v_TopGroup): ## Iters the objects
  50.             #print "i is %i and j is %i" %(i, j)
  51.             v_Array[j][3] = i ## Inserts group num in [obj][ReprodNum]
  52.             #o_Instance[j].ReprodClass = i
  53.         v_StartGroup = v_TopGroup ## Sets new obj leves start for next group
  54.         v_TopGroup = v_TopGroup + (v_NumGroups - 1) ## Sets new topgroup stop
  55.  
  56.     for i in range(0, v_NumInstances):
  57.         print v_Array[i][0],v_Array[i][1], v_Array[i][2], v_Array[i][3]
  58.  
  59. ##    for i in range(0, v_NumInstances):
  60. ##        print "Instanve[%i].ReprodClass is %i" %(i, o_Instance[i].ReprodClass)
  61.  
  62. #------------------------------
  63.  
  64.     # Functions #
  65.         # Normal Functions
  66.             # This is an list and array sort func
  67. def f_SortKey(item): ## Takes in the hidden item from the func call
  68.     return item[1] ## This is the idex to soert on
  69.  
  70. def f_FitnessAssign(v_NumInstances):
  71.     pass
  72.  
  73.         # Generator Functions
  74.  
  75.     # Classes #
  76. class c_Instance():
  77.     def __init__(self, v_Fitness, v_AS):
  78.         self.fitness = v_Fitness ## Test fitness level
  79.         self.ReprodClass = 0 ## Test Reproductive class
  80.         self.AS = v_AS ## Test ActionString
  81.  
  82.     # Main Loop #
  83. if __name__ == '__main__':
  84.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement