[英]Issue with dict() in Python, TypeError:'tuple' object is not callable
我正在嘗試創建一個函數,該函數將采用arbritrary數量的字典輸入並創建包含所有輸入的新字典。 如果兩個鍵相同,則該值應為包含兩個值的列表。 我已經成功完成了這個 - 但是,我遇到了dict()函數的問題。 如果我在python shell中手動執行dict函數,我就可以創建一個沒有任何問題的新字典; 但是,當它嵌入我的函數中時,我得到一個TypeError。 這是我的代碼如下:
#Module 6 Written Homework
#Problem 4
dict1= {'Fred':'555-1231','Andy':'555-1195','Sue':'555-2193'}
dict2= {'Fred':'555-1234','John':'555-3195','Karen':'555-2793'}
def dictcomb(*dict):
mykeys = []
myvalues = []
tupl = ()
tuplist = []
newtlist = []
count = 0
for i in dict:
mykeys.append(list(i.keys()))
myvalues.append(list(i.values()))
dictlen = len(i)
count = count + 1
for y in range(count):
for z in range(dictlen):
tuplist.append((mykeys[y][z],myvalues[y][z]))
tuplist.sort()
for a in range(len(tuplist)):
try:
if tuplist[a][0]==tuplist[a+1][0]:
comblist = [tuplist[a][1],tuplist[a+1][1]]
newtlist.append(tuple([tuplist[a][0],comblist]))
del(tuplist[a+1])
else:
newtlist.append(tuplist[a])
except IndexError as msg:
pass
print(newtlist)
dict(newtlist)
我得到的錯誤如下:
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
dictcomb(dict1,dict2)
File "C:\Python33\M6HW4.py", line 34, in dictcomb
dict(newtlist)
TypeError: 'tuple' object is not callable
如上所述,在python shell中,print(newtlist)給出:
[('Andy', '555-1195'), ('Fred', ['555-1231', '555-1234']), ('John', '555-3195'), ('Karen', '555-2793')]
如果我將此輸出復制並粘貼到dict()函數中:
dict([('Andy', '555-1195'), ('Fred', ['555-1231', '555-1234']), ('John', '555-3195'), ('Karen', '555-2793')])
輸出成為我想要的,這是:
{'Karen': '555-2793', 'Andy': '555-1195', 'Fred': ['555-1231', '555-1234'], 'John': '555-3195'}
無論我嘗試什么,我都無法在我的功能中重現這一點。 請幫幫我! 謝謝!
關鍵字不應該用作變量名稱的典型示例。 這里dict(newtlist)
試圖調用dict()
內置python,但是有一個沖突的局部變量dict
。 重命名該變量以解決問題。
像這樣的東西:
def dictcomb(*dct): #changed the local variable dict to dct and its references henceforth
mykeys = []
myvalues = []
tupl = ()
tuplist = []
newtlist = []
count = 0
for i in dct:
mykeys.append(list(i.keys()))
myvalues.append(list(i.values()))
dictlen = len(i)
count = count + 1
for y in range(count):
for z in range(dictlen):
tuplist.append((mykeys[y][z],myvalues[y][z]))
tuplist.sort()
for a in range(len(tuplist)):
try:
if tuplist[a][0]==tuplist[a+1][0]:
comblist = [tuplist[a][1],tuplist[a+1][1]]
newtlist.append(tuple([tuplist[a][0],comblist]))
del(tuplist[a+1])
else:
newtlist.append(tuplist[a])
except IndexError as msg:
pass
print(newtlist)
dict(newtlist)
你的函數有一個名為dict
的局部變量,它來自函數參數並掩蓋了內置的dict()
函數:
def dictcomb(*dict):
^
change to something else, (*args is the typical name)
你需要自己完全實現這個,還是可以使用defaultdict
? 如果是這樣,您可以執行以下操作:
from collections import defaultdict
merged_collection = defaultdict(list)
collection_1= {'Fred':'555-1231','Andy':'555-1195','Sue':'555-2193'}
collection_2= {'Fred':'555-1234','John':'555-3195','Karen':'555-2793'}
for collection in (collection_1, collection_2):
for name, number in collection.items():
merged_collection[name].append(number)
for name, number in merged_collection.items():
print(name, number)
John ['555-3195']
Andy ['555-1195']
Fred ['555-1231', '555-1234']
Sue ['555-2193']
Karen ['555-2793']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.