I'm trying to modify a Bibblesort code I'm writing that will count the number of instances and print out a seperate line for each instance and how many occurances it had.
def BubbleSortList(MyList): ComparisonCounter = 0 for j in range ( len(MyList)): for i in range ( len(MyList)-1-j ): if MyList [i+1] < MyList[i]: MyList[i] , MyList[i+1] = MyList[i+1] , MyList[i] #Swapping out the numbers. Nlist = MyList.index(MyList[j]) Count = MyList.count(MyList[j]) ComparisonCounter += 1 print "Instances for " + str(MyList[j]) + " occures " + str(Count) + " time/s" print "The comparison took place " + str(ComparisonCounter)+ " times." #Printing the results and returning MyList print "The sorted list : " + str(MyList) return Nlist def main(): FirstArgument = [100, 5, 6, 200, 12, 10, 7, 2] #First argument in the practice.. Var = BubbleSortList(FirstArgument) #Invoking BubbleSortList and assigning it to the variable Var. print #One space. SecondArgument = [200, 200, 100, 5, 6, 200, 12, 10, 6, 7, 2, 0] #Second argument in the practice Var2 = BubbleSortList(SecondArgument) #Invoking BubbleSortList and assigning it to the variable Var2 #print Var2 #Printing the results. main()
I've been trying to figure this out for a week now with no luck or progress. I know that i need to make a new list using .index where it'll help me find the first index of the instance and make a list using that index (Excuse my phrasing - i'm a NEWBY and still working on my Python lexicon).
Please I need all the help I can get. I hope my question makes sence.