2015-09-26 82 views
0

Counter.update()函數需要兩個參數(數據及其鍵),我提供了兩個參數,但它抱怨提供了三個參數。TypeError:update()需要1到2個位置參數,但給出了3個。

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 
+0

您是否正在閱讀正確的文檔:https://docs.python.org/2/library/collections.html#collections.Counter.update? Plus不要將更新調用分配給同一個變量,'update()'返回'None'。 –

回答

0

由於Counter是一類,它的所有方法都爲Counter實例的第一個參數。口譯員拿起的第三個參數是Set1,因爲Set1.update(InputString1[i], arg1)相當於Counter.update(Set1, InputString1[i], arg1)

所以,當你打電話給Set1.update時,你真的應該只傳遞一個參數,一個迭代或映射。試試這個,把數據和它的關鍵字放到一個字典中並作爲一個參數傳遞。

arg1 = InputString1.count(InputString1[i]) 
Set1 = Set1.update({InputString1[i]: arg1}) 
相關問題