简体   繁体   English

Python从数组中删除与模式匹配的元素

[英]Python remove elements matching pattern from array

I have a dictionary that contains strings as keys and lists as values. 我有一本字典,其中包含字符串作为键,并包含列表作为值。

I'd like to remove all list elements that contain the strings "food", "staging", "msatl" and "azeus". 我想删除所有包含字符串“ food”,“ staging”,“ msatl”和“ azeus”的列表元素。 I have the below code already, but am having a hard time applying the logic I have in filterIP to the rest of the strings I have. 我已经有了以下代码,但是很难将filterIP中的逻辑应用于其余的字符串。

    def filterIP(fullList):
       regexIP = re.compile(r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$')
       return filter(lambda i: not regexIP.search(i), fullList)

    groups = {key : [domain.replace('fake.com', 'env.fake.com')
      for domain in filterIP(list(set(items)))]
        for (key, items) in groups.iteritems() }

    for key, value in groups.iteritems():
      value.sort()

    meta = { "_meta" : { "hostvars" : hostvars } }
    groups.update(meta)

    print(self.json_format_dict(groups, pretty=True))

Example of current output 电流输出示例

 "role_thumper": [
    "thumper-msatl1-prod-1.env.fake.com",
    "thumper-msatl1-prod-2.env.fake.com",
    "thumper-rwva1-prod-1.env.fake.com",
    "thumper-rwva1-prod-2.env.fake.com",
    "thumper-rwva1-prod-3.env.fake.com",
    "thumper-rwva1-prod-4.env.fake.com",
    "thumper-rwva1-prod-5.env.fake.com",
    "thumper-rwva1-prod-6.env.fake.com",
    "thumper-staging-1.env.fake.com"
  ],
  "role_thumper_mongo": [
    "thumper-mongo-staging-1.env.fake.com",
    "thumper-mongo-staging-2.env.fake.com",
    "thumpermongo-rwva1-staging-1.env.fake.com",
    "thumpermongo-rwva1-staging-2.env.fake.com"
  ],
  "role_thumper_mongo_arb": [
    "thumper-mongo-arb-staging-1.env.fake.com",
    "thumpermongo-arb-rwva1-staging-1.env.fake.com"
  ],

A list comprehension is what you're after 列表理解是您所追求的

x= ["a", "b", "aa", "aba"]
x_filtered = [i for i in x if "a" not in i]
print(x_filtered)
>>> ['b']

This is just shorthand for a for loop. 这只是for循环的简写。

x_filtered = []
for i in x:
    if "a" not in i:
        x_filtered.append(i)

If I understand you correctly, this might help. 如果我对您的理解正确,这可能会有所帮助。

Set up an exclude list: 设置排除列表:

exclude=  ["food", "staging", "msatl", "azeus"]

Test list ( I couldn't really find instances in your examples) 测试列表(我无法在您的示例中真正找到实例)

test= ["food", "staging", "msatl", "azeus", "a", "bstaging"]

Run list comprehension (the name of iterators don't matter, you can pick more appropriate ones) 运行列表理解(迭代器的名称无关紧要,您可以选择更合适的迭代器)

result= [i for i in test if not any([e for e in exclude if e in i])]

result ['a'] 结果['a']

The answer above by @Julian gives a good explanation of what list comprehensions do. @Julian的上述答案很好地说明了列表理解的作用。 This uses two of them, the any part is True if there is any match in the exclude list. 这将使用其中两个,如果排除列表中存在任何匹配项,则any部分为True
Hope this helps. 希望这可以帮助。

A simple way to accomplish your task would be to iterate over each lists in the dictionary. 完成任务的一种简单方法是遍历字典中的每个列表。 Create new lists based upon your criteria, and assign the new lists to the same keys but in a new dictionary. 根据您的条件创建新列表,并将新列表分配给相同的键,但是在新的词典中。 Here is how that would look like in code: 这是代码中的样子:

def filter_words(groups, words):
    d = {}
    for key, domains in groups.iteritems():
        new_domains = []
        for domain in domains:
            if not any(word in domain for word in words):
                new_domains.append(domain)
        d[key] = new_domains
    return d

And you would call it like so: 您会这​​样称呼它:

groups = filter_words(groups, {"food", "staging", "msatl" and "azeus"})

The "meat" of the code above is the second for loop: 上面代码的“肉”是第二个for循环:

for domain in domains:
    if not any(word in domain for word in words):
        new_domains.append(domain)

This code goes over each string in the current key's list, and filters out all invalid strings according to a list of invalid words. 该代码遍历当前键列表中的每个字符串,并根据无效单词列表过滤掉所有无效字符串。

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

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