Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:';int';对象不支持项分配7_Python_Arrays_Typeerror - Fatal编程技术网

Python 类型错误:';int';对象不支持项分配7

Python 类型错误:';int';对象不支持项分配7,python,arrays,typeerror,Python,Arrays,Typeerror,这是我编写的Python3代码,用于返回列表中所有“出现”次数的值,如果没有找到,则显示所选的错误消息。然而,我得到的是: def find_duplicates(inputList, occurrences, errorMessage): '''find_duplicates(inputList, occurrences) -> Finds and returns all duplicates in list l which occur at least 'occurre

这是我编写的Python3代码,用于返回列表中所有“出现”次数的值,如果没有找到,则显示所选的错误消息。然而,我得到的是:

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
回溯(最近一次呼叫最后一次):
文件“C:\Python\Python homography.py”,第68行,在
打印(查找普通出租车号码(3))
文件“C:\Python\Python homography.py”,第56行,在find\u tritile\u taxicab\u编号中
查找重复项时(intsFound,(n),“Error”)=“Error”:
文件“C:\Python\Python homotation.py”,第32行,在find\u duplicates中
发现[当前]的发生率=0
TypeError:“int”对象不支持项分配
我可以知道错误是什么,但我不确定。我要做的是为列表中的每个不同值分别设置一个出现次数。例如,如果我有一个列表[2,2,5,7,7,7,8,8],我希望occurrencesFound[2]以2结尾,occurrencesFound[5]以1结尾,occurrencesFound[7]以4结尾,依此类推

然后,代码将检查是否有任何数字至少出现在用户要求的次数,然后返回所有出现的数字。虽然我用的方法效果不太好


我想知道的是为什么这是一个错误,以及我如何能够修复它。我试着用发生基金(curr)代替,但效果并不好。然而,这一点在报告中得到了回答。有什么想法吗?

您已在此行将occurancesFound设置为整数数据类型:

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
无法将项分配给它,因为它是整数

如果要为其分配项目,请将其设置为dict:

occurrencesFound = 0

您正在将
occurrencesFound
设置为int(
0
),然后尝试将其用作列表(
occurrencesFound[curr]=0
)。这就是问题所在。如果要将不同实体的事件存储在
发生率查找
中,请按如下方式使用:

occurancesFound = {}

这将创建一个计数(int)变量字典,其中
curr
是关键。

正如其他人所提到的,您的代码中存在严重的不一致性:您试图将
发生查找
同时用作整数和列表

在列表中查找重复项组的简单方法是使用标准模块函数。您的
find_duplicates
接受
errorMessage
arg,但我建议在调用代码中而不是在查找组的函数中进行错误处理更干净

My
find_duplicates
目录中收集组,这比使用
列表更灵活,因为它可以用于各种类型的元素,而不仅仅是整数。即使您只是收集整数组,
dict
仍然比
列表
好,除非这些整数保证大致连续,最低整数接近零(且非负)

输出

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)

我想你想做的是
occurrencesFound=[]
而不是
occurrencesFound=0
。如果你想要一本字典,甚至可能是
occurrencesFound={}
。为什么要将
occurrencesFound
设置为int?
occurrencesFound
是一个int值,因此
occurrencesFound[curr]
没有意义,你可以使用itertools.groupby在排序列表中查找重复项。
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)
{8: 3, 7: 4}