简体   繁体   English

TypeError:update()接受1到2个位置参数,但给出了3个。

[英]TypeError : update() takes from 1 to 2 positional arguments but 3 were given.

The Counter.update() function expects two arguments (data and its key) and I have provided two arguments but it complains of having provided three arguments. Counter.update()函数需要两个参数(数据及其键),我提供了两个参数,但它抱怨提供了三个参数。

from collections import Counter 
InputString1 = input()
InputString2 = input() 
Set1 = Counter()
Set2 = Counter()
for i in range(len(InputString1)):
    arg1 = InputString1.count(InputString1[i])
    Set1 = Set1.update(InputString1[i], arg1)
for i in range(len(InputString2)):
    arg2 = InputString2.count(InputString2[i])
    Set2 = Set2.update(InputString2[i], arg2)
Temp = Set1
Temp.subtract(Set2)
TotCount = sum(Temp.values())
Temp = Set2 
Temp.subtract(Set1)
TotCount = TotCount + sum(Temp.values())
print(TotCount)



Traceback (most recent call last):
  File "pallidromemake.py", line 8, in <module>
    Set1 = Set1.update(InputString1[i], arg1)
TypeError: update() takes from 1 to 2 positional arguments but 3 were given

Since Counter is a class, the first argument that all its methods take is an instance of Counter . 由于Counter是一个类,因此其所有方法都采用的第一个参数是Counter的实例。 The third argument that the interpreter is picking up is Set1 , because Set1.update(InputString1[i], arg1) is equivalent to Counter.update(Set1, InputString1[i], arg1) . 解释器选择的第三个参数是Set1 ,因为Set1.update(InputString1[i], arg1)等同于Counter.update(Set1, InputString1[i], arg1)

So you really should only be passing in one argument, an iterable or a mapping, when you call Set1.update . 因此,当您调用Set1.update时,您实际上只应该传递一个参数(可迭代或映射)。 Try this, put the data and its key into a dictionary and pass it through as one argument. 尝试此操作,将数据及其键放入字典中,并将其作为一个参数传递。

arg1 = InputString1.count(InputString1[i])
Set1 = Set1.update({InputString1[i]: arg1})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:__init__() 需要 1 到 2 个位置参数,但给出了 3 个。 尝试为我的 BMI 计算器构建 GUI - TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given. Trying to build a GUI for my BMI calculator TypeError: method() 接受 1 个位置参数,但给出了 4 个。 [使用 tkinter] - TypeError: method() takes 1 positional argument but 4 were given. [using tkinter] TypeError: on_voice_state_update() 需要 2 个位置 arguments 但给出了 3 个 - TypeError: on_voice_state_update() takes 2 positional arguments but 3 were given 类型错误:update() 需要 2 个位置参数,但给出了 3 个:Python - TypeError: update() takes 2 positional arguments but 3 were given : Python 类型错误:raw_input() 需要 1 到 2 个位置参数,但给出了 4 个 - TypeError: raw_input() takes from 1 to 2 positional arguments but 4 were given Django TypeError:url()需要2到4个位置参数,但是给出了16个 - Django TypeError: url() takes from 2 to 4 positional arguments but 16 were given 继承 TypeError: __init__() 接受 1 到 2 个位置参数,但给出了 8 个 - inheritance TypeError: __init__() takes from 1 to 2 positional arguments but 8 were given TypeError: append() 从 2 到 5 个位置 arguments 但给出了 8 个 - TypeError: append() takes from 2 to 5 positional arguments but 8 were given TypeError: with_column() 从 3 到 4 个位置 arguments 但给出了 5 个(python) - TypeError: with_column() takes from 3 to 4 positional arguments but 5 were given (python) Tkinter | TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given - Tkinter | TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM