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.

Where are you running in to trouble?

I'm trying to modify it to print out the number of instances and couldn't figure out how to do that.

my list is [200, 200, 100, 5, 6, 200, 12, 10, 6, 7, 2, 0]

and i'm trying to make it print :
the number 200 occured 3 times.
the number 100 occured 1 time.
and so on.

I'm running it on python 2.7

Note that you don't use the "j" variable. It is not necessary. Just iterate through the the list up to the next to last element. You do this over and over until there are no more swaps. In your code you loop enough times to do all of the swaps but this is not an efficient way to do it. Also using letters like i, l, and O is not a good idea as they look too much like numbers.

import random

test_list = [random.randint(1, 10) for ctr in range(20)]
print "original", test_list
found = True
swaps_ctr = 0
while found:
    swaps_ctr += 1
    found = False       ## initial state
    for ctr in range(0, len(test_list)-1):
        if test_list[ctr] > test_list[ctr+1]:
            test_list[ctr+1], test_list[ctr] = test_list[ctr], test_list[ctr+1]
            found = True     ## loop again

print swaps_ ctr, "swaps"
print test_list
for num in set(test_list): ## reduce to one entry per number
    print num, test_list.count(num)

## another way
print "-" * 20
previous_num = -1
for num in test_list: 
    if num != previous_num:
        print num, test_list.count(num)
        previous_num = num

Just as an exercise, a mild modification of woooee's code ...

''' bubble_sort_details.py
count the number of tuple swaps performed
'''

import random

def bubble_sort(mylist):
    '''mylist is sorted in place'''
    swaps_ctr = 0
    while True:
        swap_flag = False
        for ctr in range(0, len(mylist)-1):
            if mylist[ctr] > mylist[ctr+1]:
                # do a tuple swap
                mylist[ctr+1], mylist[ctr] = mylist[ctr], mylist[ctr+1]
                swaps_ctr += 1
                # print(swaps_ctr)  # test
                swap_flag = True
        # sort is done, break the while loop
        # also catches an already sorted list
        if swap_flag == False:
            return swaps_ctr


# test it with an already sorted list
#mylist = list(range(1, 11))
# or use a random list
mylist = [6, 8, 4, 10, 6, 4, 2, 6, 1, 6, 1, 9, 6, 5, 8, 5, 3, 3, 2, 7]
print("original list:")
print(mylist)
print('-'*40)

swaps = bubble_sort(mylist)

print("sorted list:")
print(mylist)
print('-'*40)
print("number of swaps:")
print(swaps)
print('-'*40)

# extra, give number frequency
for num in set(mylist):
    print("num = {}  freq = {}".format(num, mylist.count(num)))

''' result ...
original list:
[6, 8, 4, 10, 6, 4, 2, 6, 1, 6, 1, 9, 6, 5, 8, 5, 3, 3, 2, 7]
----------------------------------------
sorted list:
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 7, 8, 8, 9, 10]
----------------------------------------
number of swaps:
103
----------------------------------------
num = 1  freq = 2
num = 2  freq = 2
num = 3  freq = 2
num = 4  freq = 2
num = 5  freq = 2
num = 6  freq = 5
num = 7  freq = 1
num = 8  freq = 2
num = 9  freq = 1
num = 10  freq = 1
'''

Please note that Python by tradition uses capitalized variables for class names.

Thank you so much guys. That's awesome how quick and efficent your reply was! That's why I love this forum. :) I really appreciate the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.