简体   繁体   English

python 2.7:通过键的一部分从字典中删除键

[英]python 2.7 : remove a key from a dictionary by part of key

I have a python dictionary , the dictionary key composed from tupples, 我有一个python字典,由tupples组成的字典键,

like this : 像这样 :

{
 (u'A_String_0', u'A_String_1', u'B_String_3', u'Remove_Me'): 300, 
 (u'A_String_0', u'B_String_4'): 301, 
 (u'A_String_0', u'A_String_1', u'B_String_3', u'Remove_Key'): 301,
}

I'd like to remove all keys from dictionary when only part of tupple appears in key : 当只有部分tupple出现在key中时,我想从字典中删除所有键:

for example 'Remove_' 例如'Remove_'

In this case , must pop two keys: one contains u'Remove_Me' and another contains u'Remove_Key' 在这种情况下,必须弹出两个键:一个包含u'Remove_Me' ,另一个包含u'Remove_Key'

Finally the dictionary will look like this : 最后字典将如下所示:

{
  (u'A_String_0', u'B_String_4'): 301
}

Thanks a lot ! 非常感谢 !

One way: 单程:

    >>> d = {
     (u'A_String_0', u'A_String_1', u'B_String_3', u'Remove_Me'): 300, 
     (u'A_String_0', u'B_String_4'): 301, 
     (u'A_String_0', u'A_String_1', u'B_String_3', u'Remove_Key'): 301,
    }
    >>> 
    >>> 
    >>> d_out = {k:v for k,v in d.items() if not any(x.startswith('Remove_') for x in k)}
    >>> d_out
{(u'A_String_0', u'B_String_4'): 301}

EDIT: In case you wanted to check if Remove_ is part of any item of the tuple key, then you are better with: 编辑:如果你想检查Remove_是否是元组键的任何项目的一部分,那么你最好:

>>> d_out = {k:v for k,v in d.items() if not any('Remove_' in x for x in k)}

Since keys are always a combined thing without any structure or pattern, you always need to have the full key in order to access elements in the dictionary. 由于键总是没有任何结构或模式的组合,因此您始终需要使用完整键才能访问字典中的元素。 In particular this means that you cannot find elements using some partial key. 特别是这意味着您无法使用某个部分键找到元素。 So in order to do this, there is no way other than looking at all keys: 所以为了做到这一点,除了查看所有键之外别无他法:

>>> d = {
 (u'A_String_0', u'A_String_1', u'B_String_3', u'Remove_Me'): 300, 
 (u'A_String_0', u'B_String_4'): 301, 
 (u'A_String_0', u'A_String_1', u'B_String_3', u'Remove_Key'): 301}
>>> { k: v for k, v in d.items() if not any(x.startswith('Remove_') for x in k) }
{(u'A_String_0', u'B_String_4'): 301}

This will create a new dictionary from the source dictionary, taking every key k for which any(x.startswith('Remove_') for x in k) is not true. 这将从源字典创建一个新的字典,取每个键k any(x.startswith('Remove_') for x in k)不为真。 That any() expression will be true if there is an element in x that starts with 'Remove_' . 如果x中的元素以'Remove_'开头,则any()表达式将为true。

Using the same dictionnary, not a one-liner though. 使用相同的词典,但不是一个单行。 Also note that original post said "part of tupple appears in key" so it hasn't to be in the beginning ie startswith() 还要注意原始帖子说“tupple的一部分出现在键中”所以它不应该在开头即startswith()

>>> d = {
...      (u'A_String_0', u'A_String_1', u'B_String_3', u'Remove_Me'): 300, 
...      (u'A_String_0', u'B_String_4'): 301, 
...      (u'A_String_0', u'A_String_1', u'B_String_3', u'Remove_Key'): 301,
...     }
>>> for k in d.keys():
...     for i in k:
...         if 'Remove_' in i:
...             del d[k]
...             break
... 
>>> d
{(u'A_String_0', u'B_String_4'): 301}

if it is sartswith you have already got your answer. 如果它是sartswith你已经得到你的答案。 If not then you could use in or re for more complex checking 如果没有,那么你可以使用inre for more complex checking

Code: 码:

import re
dic = {
 (u'A_String_0', u'A_String_1', u'B_String_3', u'Remove_Me'): 300, 
 (u'A_String_0', u'B_String_4'): 301, 
 (u'A_String_0', u'A_String_1', u'B_String_3', u'Remove_Key'): 301,
}
print {k:v for k,v in dic.items() if not any(re.match("Remove", val)for val in k)} # Using in
print {k:v for k,v in dic.items() if not any("Remove" in val for val in k)} # Using re.match

Output: 输出:

{(u'A_String_0', u'B_String_4'): 301}

Note: 注意:

  • This would remover the key if Remove occurs any where in the key 如果Remove发生在密钥中的任何位置,这将Remove密钥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM