Lists inside dictionary and how to look for particular value

M

mick verdu

z={ 'PC2': ['02:02:02:02:02:02', '192.168.0.2', '200'],
'PC3': ['03:03:03:03:03:03', '192.168.0.3', '200'],
'PC1': ['01:01:01:01:01:01', '192.168.0.1', '200'] }

My solution:

z=raw_input("Enter Host, Mac, ip and time")
t=z.split()
t[0]=z[1:]
for key in dic:
if t[2] in dic[key]:
del dic[t[0]]
else:
dic[t[0]] = t[1:]


What I really want to achieve is:


How to search for a particular value inside list. First, I want the user toinput hostname and ip. e.g. PC1 and 192.168.0.1, then need to find out if 192.168.0.1 has already been assigned to some host in dictionary. In this case I would need to skip for search inside list of user input host.

Forexample, if user inputs PC1 and 192.168.0.1 i would like to skip searching in above PC1's values. So it should detect matching only with different hosts and skip its own name.

If i input PC4 and 192.168.0.1 then it should detect conflict with PC1. So PC4 would be deleted(As soon as user inputs new host it is saved in above database then if conflict with others deleted)
 
P

Peter Otten

mick said:
z={ 'PC2': ['02:02:02:02:02:02', '192.168.0.2', '200'],
'PC3': ['03:03:03:03:03:03', '192.168.0.3', '200'],
'PC1': ['01:01:01:01:01:01', '192.168.0.1', '200'] }

My solution:

z=raw_input("Enter Host, Mac, ip and time")
t=z.split()
t[0]=z[1:]
for key in dic:
if t[2] in dic[key]:
del dic[t[0]]
else:
dic[t[0]] = t[1:]


What I really want to achieve is:


How to search for a particular value inside list. First, I want the user
to input hostname and ip. e.g. PC1 and 192.168.0.1, then need to find out
if 192.168.0.1 has already been assigned to some host in dictionary. In
this case I would need to skip for search inside list of user input host.

Forexample, if user inputs PC1 and 192.168.0.1 i would like to skip
searching in above PC1's values. So it should detect matching only with
different hosts and skip its own name.

If i input PC4 and 192.168.0.1 then it should detect conflict with PC1. So
PC4 would be deleted(As soon as user inputs new host it is saved in above
database then if conflict with others deleted)

You are making the problem unnecessarily complex. For the example scenario
start with a dict that maps host to ip:

host2ip = {
"PC1": "192.168.0.1",
"PC2": "192.168.0.2",
"PC3": "192.168.0.3",
}

host, ip = raw_input("Enter host and ip: ").split()
if host not in host2ip:
print "adding", host
host2ip[host] = ip
else:
old_ip = host2ip[host]
if old_ip == ip:
print "no changes necessary"
else:
print "updating ip for", host, "from", old_ip, "to", ip
host2ip[host] = ip


Then proceed and come up with an unambiguous description of what to do with
mac and time in plain english, and add or modify data structures as
necessary.
 
T

Tim Chase

z={ 'PC2': ['02:02:02:02:02:02', '192.168.0.2', '200'],
'PC3': ['03:03:03:03:03:03', '192.168.0.3', '200'],
'PC1': ['01:01:01:01:01:01', '192.168.0.1', '200'] }

My solution:

z=raw_input("Enter Host, Mac, ip and time")
t=z.split()
t[0]=z[1:]
^
First, I don't think that this is doing what you want it to. I
suspect you want something like

data = {}
while True:
z = raw_input("Enter Host, Mac, IP and Time")
try:
host, mac, ip, time = z.split()
except ValueError:
print("Could not parse. Quitting")
break
existing = get_existing(data, mac, ip)
if existing:
print("%s/%s already exists as %s" % (
mac, ip, existing)
else:
data[host] = [mac, ip, time]
How to search for a particular value inside list. First, I want the
user to input hostname and ip. e.g. PC1 and 192.168.0.1, then need
to find out if 192.168.0.1 has already been assigned to some host
in dictionary. In this case I would need to skip for search inside
list of user input host.

You have two main choices, depending on the size of the data and how
frequently you're running the queries:

1) you can search through the entire dataset every time for any sort
of match. If the list is reasonably small or you're not throwing
thousands of queries-per-second at it, this is insignificant and can
be pretty straight-forward:

def get_existing(data, mac, ip):
for hostname, (m, i, _) in data.items():
if mac == m or ip = i:
return hostname
return None

2) You can maintain separate data structures for the
reverse-mapping. This has a much faster lookup time at the cost of
more space and maintaining the reverse mappings. The whole thing
might look more like

ip_to_hostname = {}
mac_to_hostname = {}
data = {}
while True:
z = raw_input("Enter Host, MAC, IP and Time")
try:
host, mac, ip, time = z.split()[:4]
except ValueError:
print("Could not parse. Quitting")
break
if mac in mac_to_hostname:
print("MAC already exists as %s" % mac_to_hostname[mac])
elif ip in ip_to_hostname:
print("IP already exists as %s" % ip_to_hostname[ip])
# elif host in data:
# mac2, ip2, _ = data[host]
# if (mac, ip) != (mac2, ip2):
# print("Hostname already entered (%s/%s)" % (mac2, ip2))
else:
data[host] = [mac, ip, time]
ip_to_hostname[ip] = host
mac_to_hostname[mac] = host

-tkc
 
M

mick verdu

@Peter Otten:

I have lists for keys. What I want is if host already exists it would overwrite otherwise add to database. And if host doesn't exist it will first addthis host to database and then compare its IP with IPs of rest of hosts. If ip matches with any of the other hosts, it will delete the host that it just added now.I know it doesn't make sense but I need to do it.
 
D

Denis McMahon

z={ 'PC2': ['02:02:02:02:02:02', '192.168.0.2', '200'],
'PC3': ['03:03:03:03:03:03', '192.168.0.3', '200'], 'PC1':
['01:01:01:01:01:01', '192.168.0.1', '200'] }

My solution:

z=raw_input("Enter Host, Mac, ip and time")
t=z.split()
t[0]=z[1:]
for key in dic:
if t[2] in dic[key]:
del dic[t[0]]
else:
dic[t[0]] = t[1:]


What I really want to achieve is:


How to search for a particular value inside list. First, I want the user
to input hostname and ip. e.g. PC1 and 192.168.0.1, then need to find
out if 192.168.0.1 has already been assigned to some host in dictionary.
In this case I would need to skip for search inside list of user input
host.

Forexample, if user inputs PC1 and 192.168.0.1 i would like to skip
searching in above PC1's values. So it should detect matching only with
different hosts and skip its own name.

If i input PC4 and 192.168.0.1 then it should detect conflict with PC1.
So PC4 would be deleted(As soon as user inputs new host it is saved in
above database then if conflict with others deleted)

Can we step back a few stages.

What are you writing this software for? The network management at the
level you're trying to observe happens for the most part automatically at
the ip stack / network hardware level.

Do you think this database of which ip maps to which MAC is actually
going to have some human use? If so, what?

Or is this some sort of homework exercise as part of a programming course?
 
M

mick verdu

I have programming course and trying to learn things. This is of no human use. I am just following exercises. Just have to do steps as asked.
 
M

mm0fmf

I have programming course and trying to learn things. This is of no human use. I am just following exercises. Just have to do steps as asked.

A slightly OT observation... Mick, consider using more meaningful names
than t,z etc. You know what they stand for now and you will remember
them whilst you work on this task. But if you revisit the code in a few
weeks, months etc. you'll have a hard job remembering what they stood for.
 
G

Gregory Ewing

mick said:
What I want is if host already exists it would
overwrite otherwise add to database. And if host doesn't exist it will first
add this host to database and then compare its IP with IPs of rest of hosts.
If ip matches with any of the other hosts, it will delete the host that it
just added now.

It sounds like you should be maintaining two dictionaries:

hostname --> IP (+ other host-related data)

IP --> hostname

Given a (newhost, newip) pair, first look for newip in the
IP --> hostname dictionary.

If it's there and the old hostname equals newhost, you're
finished.

If it's there with a different hostname, first delete
that entry from IP --> hostname, and also delete the
old hostname from hostname --> IP.

Now add tne new entry to both dictionaries.
 
M

mick verdu

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.
 
D

Dennis Lee Bieber

I have programming course and trying to learn things. This is of no human use. I am just following exercises. Just have to do steps as asked.

Do you have a classmate using the name "indar kumar"? Your sample data
and problem sound very similar to the recent posts by the named person --
But there is no way you are that person; the example code, the
understanding of Python data structures, and the phrasing of the posts are
much different.

{I hope this does not offend -- just that the similarity in problem
domain is so striking; the odds of two disjoint posters who do not share a
class having this problem must approach winning a lottery <G>}
 
P

Peter Otten

mick said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top