Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64790

Re: Lists inside dictionary and how to look for particular value

Date 2014-01-26 14:00 -0600
From Tim Chase <python.list@tim.thechases.com>
Subject Re: Lists inside dictionary and how to look for particular value
References <4ef4e919-2db6-4ded-9894-fd3872c3d17c@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.6002.1390766398.18130.python-list@python.org> (permalink)

Show all headers | View raw


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



Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web