jeudi 11 juin 2020

Retrieving 'x' from seaching a string against a list

I'm trying to print the value of 'x' from the dictionary (dict) after it has found a match from string (str) with any elements from the list (list).

>>> dict = {'c3750': 'switch', 'vsrx': 'vfirewall'}
>>> list = ['c3750', 'vsrx']               
>>> str = "Juniper Networks, Inc. firefly-perimeter internet router, kernel JUNOS 12.1X47-D15.4 #0: 2014-11-12 02:13:59 UTC     builder@chamuth.juniper.net:/volume/build/junos/12.1/service/12.1X47-D15.4/obj-i386/junos/bsd/kernels/VSRX/kernel Build date: 2014-11-12 "
>>> if any(x in str.lower() for x in list):        
...   print "{}".format(dict['x'])                 
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyError: 'x'

Expected result: It should return 'vfirewall'.

Aucun commentaire:

Enregistrer un commentaire