【问题标题】:modify the original list till a string is found修改原始列表直到找到一个字符串
【发布时间】:2021-03-24 04:19:03
【问题描述】:

我正在处理从早期配置收到的以下输出

['Te1/1/1', 'server', 'Ten', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']  
['Te1/1/2', 'desc', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']  
['Gi1/2/1', 'desc', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']  
['Gi2/1/2', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']  
['Te2/2/1', 'server', 'notconnect', '301', 'full', '10G', '10Gbase-LR']  
['Po120', 'notconnect', 'unassigned', 'auto', 'auto']  
['Po121', 'notconnect', '1', 'auto', 'auto']   

我想要的是从列表中删除任何字符串,直到找到字符串“已连接”或“禁用”或“未连接”,但第一项除外。

我尝试过以下配置:

regex_chk = re.compile("((?:not)*?connect(?:ed)*|disabled|)")

for item in items:
    for i in item[1:]:
        if i != regex_chk:
            item.remove(i)
    print(item)

结果如下:

['Te1/1/1']
['Te1/1/2']
['Gi1/2/1']
['Gi2/1/2']
['Te2/2/1']
['Po120']
['Po121']

而我希望结果是

['Te1/1/1', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']  
['Te1/1/2', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']  
['Gi1/2/1', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']  
['Gi2/1/2', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']  
['Te2/2/1', 'notconnect', '301', 'full', '10G', '10Gbase-LR']  
['Po120', 'notconnect', 'unassigned', 'auto', 'auto']  
['Po121', 'notconnect', '1', 'auto', 'auto']    

【问题讨论】:

  • 检查您是如何针对正则表达式进行测试的。 i != regex_chk 不这样做。
  • 只要在你的循环匹配时添加一个中断。

标签: python regex list


【解决方案1】:

首先,您应该更改正则表达式模式。
其次,当你有比赛的时候就休息。

for i in item[1:]:
    if re.match("((?:not)*?connect(?:ed)*|disabled)", i):
        break
    else:
        item.remove(i)

【讨论】:

    【解决方案2】:

    这个简单的模式适合我。 试试看

      for item in items:
         for i in item[1:]:
             if re.match('connected|notconnect|disabled', i):
                 break
             item.remove(i)
    

    【讨论】:

      【解决方案3】:

      模式((?:not)*?connect(?:ed)*|disabled|) 也匹配notconnectednotnotconnecteded 之类的字符串,并且末尾的| 使其也匹配任何其他位置。

      您也没有使用 if i != regex_chk: 中的正则表达式,它可能类似于 if not regex_chk.match(i)

      但是您的数据不需要正则表达式,您可以使用startswith,因为单词都在字符串的开头。

      items = [
          ['Te1/1/1', 'server', 'Ten', 'connected', 'trunk', 'full', '10G', '10Gbase-LR'],
          ['Te1/1/2', 'desc', 'connected', 'trunk', 'full', '10G', '10Gbase-LR'],
          ['Gi1/2/1', 'desc', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver'],
          ['Gi2/1/2', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver'],
          ['Te2/2/1', 'server', 'notconnect', '301', 'full', '10G', '10Gbase-LR'],
          ['Po120', 'notconnect', 'unassigned', 'auto', 'auto'],
          ['Po121', 'notconnect', '1', 'auto', 'auto']
      ]
      
      for item in items:
          for i in item[1:]:
              if not i.startswith(("notconnect", "connected", "disabled")):
                  item.remove(i)
                  continue
              break
          print(item)
      

      Python demo

      输出

      ['Te1/1/1', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']
      ['Te1/1/2', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']
      ['Gi1/2/1', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']
      ['Gi2/1/2', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']
      ['Te2/2/1', 'notconnect', '301', 'full', '10G', '10Gbase-LR']
      ['Po120', 'notconnect', 'unassigned', 'auto', 'auto']
      ['Po121', 'notconnect', '1', 'auto', 'auto']
      

      【讨论】:

        【解决方案4】:

        使用短的

        import re
        l = ['Te1/1/1', 'server', 'Ten', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']  
        regex_chk = re.compile("(?:not)?connect(?:ed)?|disabled")
        print(list(filter(lambda x: not regex_chk.fullmatch(x), l)))
        # ['Te1/1/1', 'server', 'Ten', 'trunk', 'full', '10G', '10Gbase-LR']
        

        Python proof

        filter(lambda x: not regex_chk.fullmatch(x), l) 获取与您的正则表达式不完全匹配的所有列表项。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多