Python Forum
Help removing asterisk item in a nested list.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help removing asterisk item in a nested list.
#1
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
Reply
#2
What have you tried? How have you thought about solving the problem, in terms of the steps (in plain English, rather than code)?
Reply
#3
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])
Reply
#4
(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']]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Whys is asterisk and random variable necessary in these functions? rrowhe4d 5 1,440 Aug-05-2022, 07:53 AM
Last Post: Gribouillis
Question Finding string in list item jesse68 8 1,800 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  Updating nested dict list keys tbaror 2 1,243 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,786 Jan-23-2022, 07:20 PM
Last Post: menator01
  how to easily create a list of already existing item CompleteNewb 15 3,382 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Remove an item from a list contained in another item in python CompleteNewb 19 5,550 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  Looping through nested elements and updating the original list Alex_James 3 2,070 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  count item in list korenron 8 3,373 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Time.sleep: stop appending item to the list if time is early quest 0 1,846 Apr-13-2021, 11:44 AM
Last Post: quest

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020