[英]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.