简体   繁体   中英

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.

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 . 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) .

So you really should only be passing in one argument, an iterable or a mapping, when you call 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})

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