Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64787 > unrolled thread
| Started by | mick verdu <mickverdu@gmail.com> |
|---|---|
| First post | 2014-01-26 10:47 -0800 |
| Last post | 2014-01-27 09:54 +0100 |
| Articles | 11 — 7 participants |
Back to article view | Back to comp.lang.python
Lists inside dictionary and how to look for particular value mick verdu <mickverdu@gmail.com> - 2014-01-26 10:47 -0800
Re: Lists inside dictionary and how to look for particular value Peter Otten <__peter__@web.de> - 2014-01-26 20:44 +0100
Re: Lists inside dictionary and how to look for particular value Tim Chase <python.list@tim.thechases.com> - 2014-01-26 14:00 -0600
Re: Lists inside dictionary and how to look for particular value mick verdu <mickverdu@gmail.com> - 2014-01-26 12:20 -0800
Re: Lists inside dictionary and how to look for particular value Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-01-27 12:08 +1300
Re: Lists inside dictionary and how to look for particular value Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-26 20:25 +0000
Re: Lists inside dictionary and how to look for particular value mick verdu <mickverdu@gmail.com> - 2014-01-26 12:28 -0800
Re: Lists inside dictionary and how to look for particular value mm0fmf <none@mailinator.com> - 2014-01-26 21:20 +0000
Re: Lists inside dictionary and how to look for particular value Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-01-26 21:36 -0500
Re: Lists inside dictionary and how to look for particular value mick verdu <mickverdu@gmail.com> - 2014-01-26 15:54 -0800
Re: Lists inside dictionary and how to look for particular value Peter Otten <__peter__@web.de> - 2014-01-27 09:54 +0100
| From | mick verdu <mickverdu@gmail.com> |
|---|---|
| Date | 2014-01-26 10:47 -0800 |
| Subject | Lists inside dictionary and how to look for particular value |
| Message-ID | <4ef4e919-2db6-4ded-9894-fd3872c3d17c@googlegroups.com> |
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)
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-01-26 20:44 +0100 |
| Message-ID | <mailman.6001.1390765477.18130.python-list@python.org> |
| In reply to | #64787 |
mick verdu wrote:
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2014-01-26 14:00 -0600 |
| Message-ID | <mailman.6002.1390766398.18130.python-list@python.org> |
| In reply to | #64787 |
On 2014-01-26 10:47, mick verdu wrote:
> 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
[toc] | [prev] | [next] | [standalone]
| From | mick verdu <mickverdu@gmail.com> |
|---|---|
| Date | 2014-01-26 12:20 -0800 |
| Message-ID | <510d7811-729f-4cf5-b563-8094745293b3@googlegroups.com> |
| In reply to | #64787 |
@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 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.I know it doesn't make sense but I need to do it.
[toc] | [prev] | [next] | [standalone]
| From | Gregory Ewing <greg.ewing@canterbury.ac.nz> |
|---|---|
| Date | 2014-01-27 12:08 +1300 |
| Message-ID | <bklis1Fihb7U1@mid.individual.net> |
| In reply to | #64791 |
mick verdu wrote:
> 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.
--
Greg
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-01-26 20:25 +0000 |
| Message-ID | <lc3qv2$gdf$3@dont-email.me> |
| In reply to | #64787 |
On Sun, 26 Jan 2014 10:47:11 -0800, mick verdu wrote:
> 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?
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | mick verdu <mickverdu@gmail.com> |
|---|---|
| Date | 2014-01-26 12:28 -0800 |
| Message-ID | <c02d9ac6-6e74-44ab-8427-28fe4f3a3f48@googlegroups.com> |
| In reply to | #64787 |
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.
[toc] | [prev] | [next] | [standalone]
| From | mm0fmf <none@mailinator.com> |
|---|---|
| Date | 2014-01-26 21:20 +0000 |
| Message-ID | <J_eFu.4958$8m7.4688@fx03.am4> |
| In reply to | #64793 |
On 26/01/2014 20:28, mick verdu wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2014-01-26 21:36 -0500 |
| Message-ID | <mailman.6017.1390790198.18130.python-list@python.org> |
| In reply to | #64793 |
On Sun, 26 Jan 2014 12:28:14 -0800 (PST), mick verdu <mickverdu@gmail.com>
declaimed the following:
>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>}
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | mick verdu <mickverdu@gmail.com> |
|---|---|
| Date | 2014-01-26 15:54 -0800 |
| Message-ID | <58821aa9-8b98-458f-a775-f79d741505cb@googlegroups.com> |
| In reply to | #64787 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-01-27 09:54 +0100 |
| Message-ID | <mailman.6034.1390812900.18130.python-list@python.org> |
| In reply to | #64804 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web