본문 바로가기
<개인공부> - IT/[Python]

복수개의 값 리스트에서 지우기 (None, multiple data remove in list)

by Aggies '19 2020. 9. 11.
반응형

네트워크 장비에 접속해 그 출력을 파싱할 때 자주 필요한 기능으로 추후 빨리 찾기 위해서 기록해 놓는다.

I use this feature really frequently when parsing network devices console's output. Basically, this post is for myself.

 

1. None 값을 지우기 (Remove None values in list) -> filter 함수 이용 (Using filter function)

 

a = ['''TenGigabitEthernet1/13 unassigned      YES TFTP   administratively down down    TenGigabitEthernet1/14 unassigned      YES TFTP   administratively down down    TenGigabitEthernet1/19 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/20 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/21 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/22 unassigned
     YES NVRAM  administratively down down    TenGigabitEthernet1/23 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/24 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/25 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/26 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/27 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/28 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/29 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/30 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/31 unassigned      YES NVRAM  administratively down down
   TenGigabitEthernet3/6  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet3/7  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet3/8  unassigned      YES NVRAM  administratively down down    Fo3/9                  unassigned
YES NVRAM  administratively down down    Fo3/10                 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/1  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/2  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/3  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/4  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/5  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/6  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/7  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/8  unassigned      YES NVRAM  administratively down down    Fo4/9                  unassigned      YES NVRAM  administratively down down    Fo4/10                 unassigned      YES NVRAM  administratively down down    mgmt0                  unassigned      YES NVRAM  administratively down down    Vlan1                  unassigned      YES NVRAM  administratively down down''']

a = a[0].split(' ')

# Remove None values
a = list(filter(None, a))

 

2. 특정 값을 지우기 (Remove certain values in list) -> list comprehension 이용 (Using list comprehension)

 

a = ['''TenGigabitEthernet1/13 unassigned      YES TFTP   administratively down down    TenGigabitEthernet1/14 unassigned      YES TFTP   administratively down down    TenGigabitEthernet1/19 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/20 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/21 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/22 unassigned
     YES NVRAM  administratively down down    TenGigabitEthernet1/23 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/24 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/25 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/26 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/27 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/28 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/29 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/30 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet1/31 unassigned      YES NVRAM  administratively down down
   TenGigabitEthernet3/6  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet3/7  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet3/8  unassigned      YES NVRAM  administratively down down    Fo3/9                  unassigned
YES NVRAM  administratively down down    Fo3/10                 unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/1  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/2  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/3  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/4  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/5  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/6  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/7  unassigned      YES NVRAM  administratively down down    TenGigabitEthernet4/8  unassigned      YES NVRAM  administratively down down    Fo4/9                  unassigned      YES NVRAM  administratively down down    Fo4/10                 unassigned      YES NVRAM  administratively down down    mgmt0                  unassigned      YES NVRAM  administratively down down    Vlan1                  unassigned      YES NVRAM  administratively down down''']

a = a[0].split(' ')
a = [elem for elem in a if 'unassigned' not in elem]
a = [1, 2, 3, 4, 5, 6, 7]

redundant = [2, 3]

a = [elem for elem in a if elem not in redundant] 
반응형