如何遍历字典以删除与字符串匹配的值?

时间:2018-12-31 12:12:44

标签: python string python-2.7 list dictionary

我想从字典中删除包含特定字符串的值,并因此删除所有具有空列表作为值的键。

mydict = {
    'Getting links from: https://www.foo.com/': 
    [
        '+---OK--- http://www.this.com/',
        '+---OK--- http://www.is.com/',
        '+-BROKEN- http://www.broken.com/',
        '+---OK--- http://www.set.com/',
        '+---OK--- http://www.one.com/'
    ],
    'Getting links from: https://www.bar.com/': 
    [
        '+---OK--- http://www.this.com/',
        '+---OK--- http://www.is.com/',
        '+-BROKEN- http://www.broken.com/'
    ],
    'Getting links from: https://www.boo.com/':
    [
        '+---OK--- http://www.this.com/',
        '+---OK--- http://www.is.com/'
    ]
}

val = "is"

for k, v in mydict.iteritems():
   if v.contains(val):
     del mydict[v]

我想要的结果是:

{
    'Getting links from: https://www.foo.com/':
    [
        '+-BROKEN- http://www.broken.com/',
        '+---OK--- http://www.set.com/',
        '+---OK--- http://www.one.com/'
    ], 
    'Getting links from: https://www.bar.com/': 
    [
        '+-BROKEN- http://www.broken.com/'
    ]
}

如何删除所有包含字符串的字典值,然后删除没有值的键?

5 个答案:

答案 0 :(得分:4)

您可以在字典理解中使用列表理解。迭代字典时,请勿更改字典中的项目数。

res = {k: [x for x in v if 'is' not in x] for k, v in mydict.items()}

# {'Getting links from: https://www.foo.com/': ['+-BROKEN- http://www.broken.com/',
#                                               '+---OK--- http://www.set.com/',
#                                               '+---OK--- http://www.one.com/'],
#  'Getting links from: https://www.bar.com/': ['+-BROKEN- http://www.broken.com/'],
#  'Getting links from: https://www.boo.com/': []}

如果您希望删除列表值为空的项目,则可以在后续步骤中进行操作:

res = {k: v for k, v in res.items() if v}

答案 1 :(得分:4)

具有简单循环:

 ${parameter#word}     Remove Smallest Prefix Pattern.  The word is expanded
                       to produce a pattern.  The parameter expansion then
                       results in parameter, with the smallest portion of the
                       prefix matched by the pattern deleted.

输出:

val = "is"

new_dict = dict()
for k, v in mydict.items():
    values = [i for i in v if val not in i]
    if values: new_dict[k] = values

print(new_dict)

答案 2 :(得分:3)

这是单线

{k: [e for e in v if val not in e] for k, v in mydict.items() if any([val not in e for e in v])}

预期输出:

Out[1]: {
    'Getting links from: https://www.bar.com/': 
    [
        '+-BROKEN- http://www.broken.com/'
    ],
    'Getting links from: https://www.foo.com/': 
    [
        '+-BROKEN- http://www.broken.com/',
        '+---OK--- http://www.set.com/',
        '+---OK--- http://www.one.com/'
    ]
}

答案 3 :(得分:0)

有两种方法可以做到。一种使用正则表达式,另一种不使用正则表达式。

如果您对正则表达式不熟悉,可以尝试以下方法:

for key, value in mydict.items():
    if val in value:
        mydict.pop(key)

输出为:

    mydict = {'Getting links from: https://www.bar.com/': ['+---OK--- http://www.this.com/',
  '+---OK--- http://www.is.com/',
  '+-BROKEN- http://www.broken.com/'],
 'Getting links from: https://www.boo.com/': ['+---OK--- http://www.this.com/',
  '+---OK--- http://www.is.com/'],
 'Getting links from: https://www.foo.com/': ['+---OK--- http://www.this.com/',
  '+---OK--- http://www.is.com/',
  '+-BROKEN- http://www.broken.com/',
  '+---OK--- http://www.set.com/',
  '+---OK--- http://www.one.com/']}

答案 4 :(得分:0)

使用dict comprehension,您可以尝试以下操作:

import re

val = 'is'

# step 1 - remove line having is
mydict = {k:[re.sub(r'.*is*.', '', x) for x in v] for k,v in mydict.items()}

# filtering out keys if there is no value - if needed
mydict = {k:v for k,v in mydict.items() if len(v) > 0}

print(mydict)

{'Getting links from: https://www.foo.com/': ['com/',
  'com/',
  '+-BROKEN- http://www.broken.com/',
  '+---OK--- http://www.set.com/',
  '+---OK--- http://www.one.com/'],
 'Getting links from: https://www.bar.com/': ['com/',
  'com/',
  '+-BROKEN- http://www.broken.com/'],
 'Getting links from: https://www.boo.com/': ['com/', 'com/']}