0

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. I did try and use List Comprehension but struggled to get the output i needed.

much appericated

Hooch_dgb
  • 13
  • 5

4 Answers4

1

Try this:

substrings = ['vlan-480', 'vlan-484']

filter_object = filter(lambda a: any(x in a for x in substrings), staticPaths)

print(list(filter_object))

The list substrings contains substrings to search for

The output I get for your dataset is:

['[N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-480]', "['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-484]", "['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C01-N2][vlan-480]"]
Almog-at-Nailo
  • 1,152
  • 1
  • 4
  • 20
0

You can also use list comprehension

substrings = ['vlan-480', 'vlan-484']

path_matches = [x for x in staticPaths if substrings[0] in x or substrings[1] in x]

that results in :

path_matches

['[N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-480]',
"['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C02-N2][vlan-484]",
"['N4-NFS-NETAPP-8040-C01]/[N4-NHT-LEAF-VPC-FAS-C01-N2][vlan-480]"]
Hermes Morales
  • 593
  • 6
  • 17
-1

Your problem is with indentation i guess check this

path_matches = []

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

Try to use this:

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

print(list(filter_object))

To do multi selection you can use operator OR/AND, so it will be such this 'vlan-480' in a or 'vlan-484' in a

Lakerr
  • 97
  • 4