我正在尝试创建一个函数,该函数将采用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'}
无论我尝试什么,我都无法在我的功能中重现这一点。请帮帮我!谢谢!
答案 0 :(得分:8)
不应将关键字用作变量名称的典型示例。这里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)
答案 1 :(得分:4)
你的函数有一个名为dict
的局部变量,它来自函数参数并掩盖了内置的dict()
函数:
def dictcomb(*dict):
^
change to something else, (*args is the typical name)
答案 2 :(得分:0)
您是否需要完全自己实现,或者可以使用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']