0

I need help incorporating a find_duplicates function in a program. It needs to traverse the list checking every element to see if it matches the target. Keep track of how many matches are found and print out a sentence summary.

Below is the code that I have written thus far:

myList = [69, 1 , 99, 82, 17]

#Selection Sort Function 

def selection_sort_rev(aList):
    totalcomparisions = 0
    totalexchanges = 0
    p=0
    print("Original List:" , aList)
    print()
    n = len(aList)
    for end in range(n, 1, -1):      
        comparisions = 0
        exchanges = 1
        p= p + 1

#Determine Largest Value

        max_position = 0
        for i in range(1, end):
            comparisions = comparisions + 1
            if aList[i] < aList[max_position]:   
                max_position = i

#Passes and Exchanges 
        exchnages = exchanges + 1
        temp = aList [end - 1]       
        aList [end - 1] = aList [max_position]
        aList [max_position] = temp
        print ("Pass", p,":", "Comparsions:", comparisions, "\tExchanges:" , exchanges)
        print("\t", aList, "\n")
        totalcomparisions = totalcomparisions + comparisions
        totalexchanges = totalexchanges + exchanges

    print("\tTotal Comparisons:", totalcomparisions, "\tTotal Exchanges:", totalexchanges)
    return 

The find_duplicates function has to have:

  • Input paramaters: List, target (value to find)
  • Search for the target in the list and count how many times it occurs
  • Return value: None

I'm sure there are more efficient ways of doing this but I am a beginner to programming and I would like to know how to do this in the most simple way possible. PLEASE HELP!!!

user3321010
  • 49
  • 1
  • 5
  • possible duplicate of [Find and list duplicates in Python list](http://stackoverflow.com/questions/9835762/find-and-list-duplicates-in-python-list) – halex Mar 01 '14 at 21:24
  • umm... Why are you posting code for a selection sort, when you want to find the duplicates in a list? – inspectorG4dget Mar 01 '14 at 21:24

2 Answers2

0
import collections

def find_duplicates(L, target):
    i_got_this_from_stackoverflow = collections.Counter(L)
    if target not in i_got_this_from_stackoverflow:
        print "target not found"
    else:
        print i_got_this_from_stackoverflow[target], "occurrences of the target were found"
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
0

If you are looking to improve your programming skills this is a great discussion. If you just want the answer to your problem, use the list.count() method, which was created to answer precisely that question:

>>> myList = [69, 1 , 99, 82, 17,  1, 82]
>>> myList.count(82)
2

Also, your test code does not contain any duplicates in the list. This is a necessary test case, but you also need to try your code on at least one list with duplicates to test that is works when they are present.

When you are learning and starting to achieve mastery you can save yourself an awful lot of time by learning how the various components work. If this is an assignment, however, keep on truckin'!

holdenweb
  • 33,305
  • 7
  • 57
  • 77