Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的dict()问题,TypeError:';元组';对象不可调用_Python_Function_Dictionary_Tuples_Typeerror - Fatal编程技术网

Python中的dict()问题,TypeError:';元组';对象不可调用

Python中的dict()问题,TypeError:';元组';对象不可调用,python,function,dictionary,tuples,typeerror,Python,Function,Dictionary,Tuples,Typeerror,我正在尝试创建一个函数,该函数将接受任意数量的字典输入,并创建一个包含所有输入的新字典。如果两个键相同,则该值应为包含这两个值的列表。我成功地做到了这一点——但是,dict()函数有问题。如果我在pythonshell中手动执行dict函数,我就能够毫无问题地创建一个新字典;然而,当这嵌入到我的函数中时,我得到一个TypeError。下面是我的代码: #Module 6 Written Homework #Problem 4 dict1= {'Fred':'555-1231','Andy':'

我正在尝试创建一个函数,该函数将接受任意数量的字典输入,并创建一个包含所有输入的新字典。如果两个键相同,则该值应为包含这两个值的列表。我成功地做到了这一点——但是,dict()函数有问题。如果我在pythonshell中手动执行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
如果我将此输出复制并粘贴到dict()函数中:

dict([('Andy', '555-1195'), ('Fred', ['555-1231', '555-1234']), ('John', '555-3195'), ('Karen', '555-2793')])
def dictcomb(*dict):
              ^
            change to something else, (*args is the typical name)
输出成为我想要的,即:

{'Karen': '555-2793', 'Andy': '555-1195', 'Fred': ['555-1231', '555-1234'], 'John': '555-3195'}

无论我尝试什么,我都无法在我的功能中重现这一点。请帮帮我!谢谢大家!

关键字不应用作变量名的典型示例。这里,
dict(newtlist)
试图调用python中的
dict()
内置变量,但存在冲突的局部变量
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()
函数:

dict([('Andy', '555-1195'), ('Fred', ['555-1231', '555-1234']), ('John', '555-3195'), ('Karen', '555-2793')])
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']