Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'skip:[ 20': 0.04; 'elif': 0.05; '%s"': 0.09; 'data:': 0.09; 'host,': 0.09; 'lookup': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'def': 0.12; '-tkc': 0.16; 'dictionary.': 0.16; 'existing:': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'hostname': 0.16; 'reasonably': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'entered': 0.20; 'input': 0.22; 'separate': 0.22; 'exists': 0.24; 'skip': 0.24; 'sort': 0.25; 'first,': 0.26; 'header:In-Reply-To:1': 0.27; 'host': 0.29; "skip:' 10": 0.31; 'maintaining': 0.32; 'running': 0.33; 'mac': 0.33; 'except': 0.35; 'something': 0.35; 'doing': 0.36; 'charset :us-ascii': 0.36; 'two': 0.37; 'list': 0.37; 'list.': 0.37; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'space': 0.40; 'how': 0.40; 'break': 0.61; 'entire': 0.61; "you're": 0.61; 'more': 0.64; 'frequently': 0.68; 'reverse': 0.68; '192.168.0.1': 0.84; 'choices,': 0.84; 'received:50.22': 0.84; 'subject:Lists': 0.91 Date: Sun, 26 Jan 2014 14:00:43 -0600 From: Tim Chase To: python-list@python.org Subject: Re: Lists inside dictionary and how to look for particular value In-Reply-To: <4ef4e919-2db6-4ded-9894-fd3872c3d17c@googlegroups.com> References: <4ef4e919-2db6-4ded-9894-fd3872c3d17c@googlegroups.com> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Authenticated-Sender: tim@thechases.com X-OutGoing-Spam-Status: No, score=-3.5 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390766398 news.xs4all.nl 2858 [2001:888:2000:d::a6]:39574 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64790 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