修改原始列表,直到找到字符串

2024-06-08 01:09:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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

['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']    

Tags: noautoitemfulltrunktransceiverlrdisabled
3条回答

首先,您应该更改正则表达式模式。
第二,当你有比赛时就休息一下

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

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

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

模式((?: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']

相关问题 更多 >

    热门问题