简体   繁体   中英

TypeError: 'int' object does not support item assignment 7

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?

You have occurancesFound set to an integer data type at this line:

occurrencesFound = 0

You cannot assign an item to it because it is an integer.

If you want to assign items to it, make it a dict:

occurancesFound = {}

You're setting occurrencesFound to an int ( 0 ) and then trying to use it as a list ( occurrencesFound[curr] = 0 ). That is the problem. If you want to store occurences of different entities in occurrencesFound , use it as follows:

occurrencesFound = {}
occurrencesFound[curr] = 0

This will create a dictionary of count (int) variables, where curr is the key.

As others have mentioned there's a serious inconsistency in your code: you are trying to use occurrencesFound both as an integer and a list.

The simple way to find groups of duplicate items in a list is to use the standard module function itertools.groupby . Your find_duplicates takes an errorMessage arg, but I advise that it's cleaner to do the error handling in the calling code rather than in the function that finds the groups.

My find_duplicates collects the groups in a dict , which is more flexible that using a list , since it can be used for various types of elements, not just integers. And even if you're just collecting groups of integers, a dict is still better than a list , unless those integers are guaranteed to be roughly contiguous, with the lowest integer near zero (and non-negative).

from itertools import groupby

def find_duplicates(input_list, occurrences=2):
    input_list = sorted(input_list)
    groups = {}
    for k, g in groupby(input_list):
        # We have to convert iterator `g` to a list to get its length
        glen = len(list(g))
        if glen >= occurrences:
            groups[k] = glen
    return groups

# Test
input_list = [7, 8, 7, 2, 8, 5, 7, 7, 8, 2]

groups = find_duplicates(input_list, 3)
if not groups:
    print('No groups found')
else:
    print(groups)

output

{8: 3, 7: 4}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM