Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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字典中的键_Python_Python 2.7_Dictionary - Fatal编程技术网

删除python字典中的键

删除python字典中的键,python,python-2.7,dictionary,Python,Python 2.7,Dictionary,如果看起来是复制品,请原谅。我已使用和提供的方法 我使用的Python版本是2.7.3。我将一个字典传递到一个函数中,如果条件为true,我希望删除键 当我查字典前后的长度是一样的 我的代码是: def checkDomain(**dictArgum): for key in dictArgum.keys(): inn=0 out=0 hel=0 pred=dictArgum[key] #it

如果看起来是复制品,请原谅。我已使用和提供的方法

我使用的Python版本是2.7.3。我将一个字典传递到一个函数中,如果条件为true,我希望删除键

当我查字典前后的长度是一样的

我的代码是:

def checkDomain(**dictArgum):

    for key in dictArgum.keys():

         inn=0
         out=0
         hel=0
         pred=dictArgum[key]

         #iterate over the value i.e pred.. increase inn, out and hel values

         if inn!=3 or out!=3 or hel!=6:
                   dictArgum.pop(key, None)# this tried
                   del dictArgum[key] ###This also doesn't remove the keys 

print "The old length is ", len(predictDict) #it prints 86

checkDomain(**predictDict) #pass my dictionary

print "Now the length is ", len(predictDict) #this also prints 86

此外,我请求您帮助我了解如何回复回复。每次我都不能正确回答。换行符或编写代码对我不起作用。谢谢。

发生这种情况是因为字典已解包并重新打包到关键字参数
**dictArgum
,因此您在函数中看到的字典是另一个对象

>>> def demo(**kwargs):
    print id(kwargs)


>>> d = {"foo": "bar"}
>>> id(d)
50940928
>>> demo(**d)
50939920 # different id, different object
相反,直接传递字典:

def checkDomain(dictArgum): # no asterisks here

    ...

print "The old length is ", len(predictDict)

checkDomain(predictDict) # or here
返回
并分配它:

def checkDomain(**dictArgum):

    ...

    return dictArgum # return modified dict

print "The old length is ", len(predictDict)

predictDict = checkDomain(**predictDict) # assign to old name

你所说的“理解如何回复回复”是什么意思?如果你有一个关于它自己的问题,考虑一下是的,你不能写多行评论。编辑您的问题以添加重要信息,包括代码。但是你可以对
内联单空格代码段使用反勾号
@jornsharpe嗨,我的回复中没有正确的缩进和换行。我可以适当地发布问题,但在回复评论时有困难。!:(@LevLevitsky是对的-注释不是放置代码的正确位置。@LevLevitsky-代码已编辑。抱歉。