for v in temp_var_dict['PSE2']:
#print(v)
match = str(match)
match = match.strip("[]")
match = match.strip("''")
print(f'** match={match} v={v} **')
result = [index for index, value in enumerate(v) if match in value]当比较下面的结果时,我希望看到匹配。在我看来,match=1/2和v=1/2/CPU0应该是匹配的,并且设置'result'=0。然而,这并没有发生。请在正确的语法上给出建议。
** match=1/2 v=0/7/CPU0 **
** match=1/2 v=1/0/CPU0 **
** match=1/2 v=1/2/CPU0 ** defaultdict(,{'PSE1':'0/0/CPU0','1/3/CPU0','PSE2':'0/7/CPU0','1/0/CPU0','1/2/CPU0','IF_PSE1':['TenGigE1/2/0/25.201','PSE2=NO',25,'TenGigE1/2','TenGigE1/2/0/25.201','PSE2=NO',‘TenGigE1/2/0/25.201’,‘PSE2=NO’,‘TenGigE1/2/0/25.201’,‘PSE2=NO’,25,'TenGigE1/2','REPLICATION=NO','TenGigE1/2/0/25.201','PSE2=NO',25,'TenGigE1/2','REPLICATION=NO']})
发布于 2019-04-11 13:23:14
我认为,如果有匹配,您要查找的是子字符串的起始索引,但不是完全确定。例如,如果v= '1/2/CPU0‘且match = '1/2’,则存在匹配,并且子字符串'1/2‘从索引'0’开始,因此result=0
我不确定您的逻辑中出了什么问题,但在这种情况下,str.find(子字符串)是您的朋友。如果未找到匹配项,则返回-1;如果找到匹配项,则返回子字符串的起始索引。
请看下面代码的修改后的“工作”版本:
temp_var_dict = {'PSE1': ['0/0/CPU0', '1/3/CPU0'], 'PSE2': ['0/7/CPU0', '1/0/CPU0', '1/2/CPU0'], 'IF_PSE1': [['TenGigE1/2/0/25.201', 'PSE2=NO', 25, 'TenGigE1/2', 'REPLICATION=NO'], ['TenGigE1/2/0/25.201', 'PSE2=NO', 25, 'TenGigE1/2', 'REPLICATION=NO'], ['TenGigE1/2/0/25.201', 'PSE2=NO', 25, 'TenGigE1/2', 'REPLICATION=NO']]}
match = "1/2"
for v in temp_var_dict['PSE2']:
match = str(match)
match = match.strip("[]")
match = match.strip("''")
result = v.find(match)
print "match={%s} v={%s} result={%s}" % (match,v,str(result))https://stackoverflow.com/questions/55624883
复制相似问题