[英]Finding elements of a list and printing it out
i am still learning python and i am struggling to find a way to get this to work the way i want.我仍在学习 python,我正在努力寻找一种方法让它按照我想要的方式工作。
#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.因此,我在这里尝试做的是过滤掉与“vlan-480”匹配的任何内容并返回整行,例如,如果我运行该代码,我会收到正确的输出。 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]这将是 - [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.然而,在 lambda 函数中状态 'vlan-480' 在哪里我实际上想将它传递给一个 LIST,但是因为我使用的是“in”语句,它只允许我传递一个字符串。
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我再次想检查倍数,例如,给我“vlan-480”和“vlan-484”的输出,它应该从 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.还尝试了 if 语句,但我有同样的问题,使用单字符串选项。
#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.我确实尝试过使用 List Comprehension,但很难获得我需要的输出。
much appericated非常喜欢
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列表substrings
包含要搜索的substrings
字符串
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]"]
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]"]
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)
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要进行多选,您可以使用运算符 OR/AND,因此 a 中的 'vlan-480' 或 a 中的 'vlan-484' 就是这样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.