Lists inside dictionary and how to look for particular value
Peter Otten
__peter__ at web.de
Mon Jan 27 03:54:49 EST 2014
mick verdu wrote:
> ThanK you. It solved my problem.
> Can someone tell me how can i print particular value inside list of key.
>
> I know how to print J['PC2'][1] means will print IP. but I want the user
> to input some element and I will print element just before that element.
>
> e.g. if user inputs 192.168.0.2, program will print 02:02:02:02:02:02.
> If user inputs 192.168.0.1 I will print 01:01:01:01:01:01.
IP_INDEX = 1
MAC_INDEX = 0
record = J["PC2"]
def is_ip(s):
return "." in s
def is_mac(s):
return ":" in s
s = raw_input("Enter MAC or IP: "
if is_mac(s):
print record[IP_INDEX]
elif is_ip(s):
print record[MAC_INDEX]
else:
print "not a MAC or IP"
You can of course replace the is_ip() and is_mac() implementation with
better ones.
More information about the Python-list
mailing list