[英]Issue with dict() in Python, TypeError:'tuple' object is not callable
I'm trying to make a function that will take an arbritrary number of dictionary inputs and create a new dictionary with all inputs included. 我正在尝试创建一个函数,该函数将采用arbritrary数量的字典输入并创建包含所有输入的新字典。 If two keys are the same, the value should be a list with both values in it. 如果两个键相同,则该值应为包含两个值的列表。 I've succeded in doing this-- however, I'm having problems with the dict() function. 我已经成功完成了这个 - 但是,我遇到了dict()函数的问题。 If I manually perform the dict function in the python shell, I'm able to make a new dictionary without any problems; 如果我在python shell中手动执行dict函数,我就可以创建一个没有任何问题的新字典; however, when this is embedded in my function, I get a TypeError. 但是,当它嵌入我的函数中时,我得到一个TypeError。 Here is my code below: 这是我的代码如下:
#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)
The error I get is as follows: 我得到的错误如下:
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
As I described above, in the python shell, print(newtlist) gives: 如上所述,在python shell中,print(newtlist)给出:
[('Andy', '555-1195'), ('Fred', ['555-1231', '555-1234']), ('John', '555-3195'), ('Karen', '555-2793')]
If I copy and paste this output into the dict() function: 如果我将此输出复制并粘贴到dict()函数中:
dict([('Andy', '555-1195'), ('Fred', ['555-1231', '555-1234']), ('John', '555-3195'), ('Karen', '555-2793')])
The output becomes what I want, which is: 输出成为我想要的,这是:
{'Karen': '555-2793', 'Andy': '555-1195', 'Fred': ['555-1231', '555-1234'], 'John': '555-3195'}
No matter what I try, I can't reproduce this within my function. 无论我尝试什么,我都无法在我的功能中重现这一点。 Please help me out! 请帮帮我! Thank you! 谢谢!
A typical example of why keywords should not be used as variable names. 关键字不应该用作变量名称的典型示例。 Here dict(newtlist)
is trying to call the dict()
builtin python, but there is a conflicting local variable dict
. 这里dict(newtlist)
试图调用dict()
内置python,但是有一个冲突的局部变量dict
。 Rename that variable to fix the issue. 重命名该变量以解决问题。
Something like this: 像这样的东西:
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)
You function has a local variable called dict
that comes from the function arguments and masks the built-in dict()
function: 你的函数有一个名为dict
的局部变量,它来自函数参数并掩盖了内置的dict()
函数:
def dictcomb(*dict):
^
change to something else, (*args is the typical name)
Do you need to implement this entirely yourself, or would it be okay to use defaultdict
? 你需要自己完全实现这个,还是可以使用defaultdict
? If so, you might do something like: 如果是这样,您可以执行以下操作:
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.