Python Forum
Help removing asterisk item in a nested list. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help removing asterisk item in a nested list. (/thread-25573.html)



Help removing asterisk item in a nested list. - bmcguire - Apr-03-2020

I have a script that successfully accesses a network switch and retrieves a list of vlans and associated ports.
The live ports have an asterisk at the end.

I need to remove the asterisks.

Here is the list:

nvlanlist = [['VLAN_2344', 'ae0.0*'], ['VLAN_2344', 'ae1.0*'], ['VLAN_2344', 'ge-0/0/1.0'], ['VLAN_2344', 'ge-0/0/10.0*'], ['VLAN_2344', 'ge-0/0/11.0'], ['VLAN_2344', 'ge-0/0/12.0*'], ['VLAN_2344', 'ge-0/0/13.0*']]

Rather than showing all my failed attempts, I'm just asking for help.

Python 3.8.2


RE: Help removing asterisk item in a nested list. - ndc85430 - Apr-03-2020

What have you tried? How have you thought about solving the problem, in terms of the steps (in plain English, rather than code)?


RE: Help removing asterisk item in a nested list. - bmcguire - Apr-06-2020

I've tried replace with nothing and remove.

After some sleep and not looking at it for two days the correct result was easy. Replace worked. I was trying to replace all asterisks in the list at once. Instead of going through them one at at time.

Here is my final code that producing a list without the asterisks.

from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from pprint import pprint
import getpass,csv,sys,pprint
from jnpr.junos.factory.factory_loader import FactoryLoader
import yaml,sys
from jnpr.junos.op.ethport import EthPortTable


z=0

yaml.warnings({'YAMLLoadWarning': False})
with open("lldp-neighbors.yml", 'r') as tvs:
globals().update(FactoryLoader().load(yaml.load(tvs)))
#host = input("Host :")
host = '10.44.2.3'

username=raw_input("\nEnter your device username: ")
password=getpass.getpass(prompt="\nEnter your device password: ")
nvlanlist=[]
count=0
#with Device(host=host, user='enumber', password='password',gather_facts=True) as dev:
with Device(host=host, user=username, password=password,gather_facts=True) as dev:

vlanlocal = VlanInformationTable(dev)
vlanlocalELS=VlanInformationTableELS(dev)
vlanlocal.get()
vlanlocalELS.get()
model = (dev.facts['model'])

if '4600' in model or '3400' in model or '4300' in model or '2300' in model:
for item in vlanlocalELS:
if item.tag != '0':
for y in item.members:
if len(y) == 1:
if count ==0:
nvlanlist.append([item.name,item.members.replace('*','')])
count = count +1
else:
nvlanlist.append([item.name,item.members[z].replace('*','')])
z=z+1
z=0
else:
for item in vlanlocal:
if item.tag != '0':
count = 0
for y in item.members:
if len(y) == 1:
if count ==0:
nvlanlist.append([item.name,item.members.replace('*','')])
count = count +1
else:
nvlanlist.append([item.name,item.members[z].replace('*','')])
z=z+1
z=0
for item in nvlanlist:
print (item[0]+','+item[1])

Sorry here is my code correctly entered into the thread.
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from pprint import pprint
import getpass,csv,sys,pprint
from jnpr.junos.factory.factory_loader import FactoryLoader
import yaml,sys
from jnpr.junos.op.ethport import EthPortTable
import re

z=0

yaml.warnings({'YAMLLoadWarning': False})
with open("lldp-neighbors.yml", 'r') as tvs:
    globals().update(FactoryLoader().load(yaml.load(tvs)))
#host = input("Host :")
host = '10.44.2.3'

#username=raw_input("\nEnter your device username: ")
username="e061462"
#password=getpass.getpass(prompt="\nEnter your device password: ")
password="1970.Bmac8"

nvlanlist=[]
string="*"
count=0
#with Device(host=host, user='enumer', password='password',gather_facts=True) as dev:
with Device(host=host, user=username, password=password,gather_facts=True) as dev:

    vlanlocal = VlanInformationTable(dev)
    vlanlocalELS=VlanInformationTableELS(dev)
    vlanlocal.get()
    vlanlocalELS.get() 
    model = (dev.facts['model'])

    if '4600' in model or '3400' in model or '4300' in model or '2300' in model:
        for item in vlanlocalELS:
            if item.tag != '0':
                    for y in item.members:
                        if len(y) == 1:
                            if count ==0:
                                nvlanlist.append([item.name,item.members.replace('*','')])
                                count = count +1
                        else:
                            nvlanlist.append([item.name,item.members[z].replace('*','')])
                            z=z+1
                    z=0
    else:
        for item in vlanlocal:
            if item.tag != '0':
                    count = 0
                    for y in item.members:
                        if len(y) == 1:
                            if count ==0:
                                nvlanlist.append([item.name,item.members.replace('*','')])
                                count = count +1
                        else:
                            nvlanlist.append([item.name,item.members[z].replace('*','')])
                            z=z+1
                    z=0
for item in nvlanlist:
    print (item[0]+','+item[1])



RE: Help removing asterisk item in a nested list. - snippsat - Apr-06-2020

(Apr-06-2020, 12:42 PM)bmcguire Wrote: Here is my final code that producing a list without the asterisks.
Thanks for showing the solution that worked for you.
To show another way.
>>> [[e.strip('*') for e in element] for element in nvlanlist]
[['VLAN_2344', 'ae0.0'],
 ['VLAN_2344', 'ae1.0'],
 ['VLAN_2344', 'ge-0/0/1.0'],
 ['VLAN_2344', 'ge-0/0/10.0'],
 ['VLAN_2344', 'ge-0/0/11.0'],
 ['VLAN_2344', 'ge-0/0/12.0'],
 ['VLAN_2344', 'ge-0/0/13.0']]