Help with Python Script

Hi, i am still learning python and i am struggling to find a way to get this to work the way i want.

#First Method

staticPaths = [
    "[N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-480]", 
    "[N4-OLDCLOUD-IPSTORAGE/epg-N4-NFS-NETAPP-8040-C01]/[vlan-481]]",
    "['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-484]",
    "['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-485]",
    "['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C01-N2][vlan-480]"
]
for path in staticPaths:

   filter_object = filter(lambda a: 'vlan-480' in a, staticPaths)

print(list(filter_object))

So what i am trying to do here is filter out anything that matches ‘vlan-480’ and return the entire line, so for example, if i run that code, i receive the correct output. which would be -
[N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C01-N2][vlan-480]
['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-480]

However where is states ‘vlan-480’ in the lambda function i actually want to pass it a LIST but because i am using the “in” statement, it only allows me to pass a single string.

Again i want to check multiples, so for example, give me the output for ‘vlan-480’ AND ‘vlan-484’ and it should return the lines for me from the staticPaths

I cannot think of way of getting this done, might just be me been stupid but for some reason i cannot solve it.

Also tried an if statement but i have the same problem, with the single string option.

#Second Method

path_matches = []

for path_match in staticPaths:
  if 'vlan-480' in path_match:
     path_matches.append(path_match)

print(path_matches)

Can anyone think of a way of doing this, its probably really easy but for some reason i cannot think of it.

much appericated

1 Like

First of all check if you have every path in double quotes and you have them all closed inside the [] list brackets :slight_smile: Check out this code:

staticPaths = [
    "[N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-480]", 
    "[N4-OLDCLOUD-IPSTORAGE/epg-N4-NFS-NETAPP-8040-C01]/[vlan-481]]",
    "['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-484]",
    "['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-485]",
    "['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C01-N2][vlan-480]"
]

# First method
for path in staticPaths:
    filter_object = filter(lambda a: "vlan-480" in a, staticPaths)
print(list(filter_object))

path_matches = []

print(20 * "*")

# Second method
for path_match in staticPaths:
    if "vlan-480" in path_match:
        path_matches.append(path_match)


print(path_matches)

And, if I may, for posting your next problems I strongly suggest to use preformatted text (this icon </>) so You could show us the exact code you are dealing with, just like I did here :slight_smile: Python does not forgive any indentation mistakes :wink: I hope this will help! Keep up the good work! :star_struck:

hi, thanks for the reply.

are you just asking me to format correctly or solving my issue ?

thanks

Only to format :slight_smile: It is just very hard to get what is wrong with the code if I (or anybody) can`'t see how it is formatted. Thats all :wink:

okay sorted, hopefully easier to read

1 Like

Great! It looks very good now :grinning: