def find_duplicates(inputList, occurrences, errorMessage):
'''find_duplicates(inputList, occurrences) -> Finds and returns all duplicates in list l
which occur at least 'occurrences' times
If none are found then errorMessage is returned'''
curr = 0
prev = 0
occurrencesFound = 0
duplesFound = []
inputList = sorted(inputList)
print(inputList)
for i in range(len(inputList)):
prev = curr
curr = inputList[i]
occurrencesFound[curr] = 0
if curr == prev and duplesFound.count(curr) == 0:
occurrencesFound[curr] += 1
if occurrencesFound[curr] == occurrences:
duplesFound.append(curr)
occurrencesFound = 0
if duplesFound == []:
duplesFound = errorMessage
return duplesFound
This is Python 3 code I wrote to return all the values in a list that occur 'occurrences' times, and display a chosen error message if none were found. However, this is what I'm getting:
Traceback (most recent call last):
File "C:\Python\Python Homework.py", line 68, in <module>
print(find_trivial_taxicab_numbers(3))
File "C:\Python\Python Homework.py", line 56, in find_trivial_taxicab_numbers
while find_duplicates(intsFound, (n), "Error") == "Error":
File "C:\Python\Python Homework.py", line 32, in find_duplicates
occurrencesFound[curr] = 0
TypeError: 'int' object does not support item assignment
I can somewhat tell what the error is, but I'm not sure. What I'm trying to do is to have a separate number of occurrences for each different value in the list. For example, if I had a list [2,2,5,7,7,7,7,8,8,8] I would want occurrencesFound[2] to end up as 2, occurrencesFound[5] to end up as 1, occurrencesFound[7] to end up as 4, and so on.
With that, the code would then check if any numbers occurred at least the number of times the user asked for, and then return all the numbers that did. The method I used didn't work great, though...
What I would like to know is why this is an error and how I might be able to fix it. I tried doing occurrencesFound(curr) instead, and that worked no better. That was answered in "TypeError: 'function' object does not support item assignment" however. Any ideas?
occurrencesFound = []and notoccurrencesFound = 0. Or maybe evenoccurrencesFound = {}if you want a dictionary. - idjawoccurrencesFoundto an int? - user2357112 supports MonicaoccurencesFoundis a int value, sooccurrencesFound[curr]doesn't make sense - OneCricketeer