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


Groups > comp.lang.python > #56736

Re: Why isn't this code working how I want it to?

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'duplicate': 0.07; 'subject:code': 0.07; 'planned.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Why': 0.09; 'dict': 0.16; 'keys:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'slight': 0.16; 'values?': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'variable': 0.18; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; "i've": 0.25; 'code:': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'wonder': 0.29; "doesn't": 0.30; 'work.': 0.31; 'keys': 0.31; 'becomes': 0.33; "i'd": 0.34; 'could': 0.34; 'basic': 0.35; 'problem.': 0.35; 'but': 0.35; 'version': 0.36; 'subject:?': 0.36; 'apple': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'does': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'email addr:gmail.com': 0.63; 'due': 0.66; 'status:': 0.68; 'obvious': 0.74; 'subject:this': 0.83; 'subject:want': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Why isn't this code working how I want it to?
Date Sat, 12 Oct 2013 11:20:24 +0200
Organization None
References <f74fadac-e7c3-4419-9a65-ed6ecd5eb3fe@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50848404.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1032.1381569606.18130.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1381569606 news.xs4all.nl 15892 [2001:888:2000:d::a6]:40394
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:56736

Show key headers only | View raw


reubennottage@gmail.com wrote:

> I've been working on a program and have had to halt it due a slight
> problem. Here's a basic version of the code:
> 
> a = 'filled'
> b = 'filled'
> c = 'empty'
> d = 'empty'
> e = 'filled'
> f = 'empty'
> g = 'filled'
> 
> testdict = {a : 'apple' , b : 'banana' , c : 'cake' , d : 'damson' , e :
> 'eggs' , f : 'fish' , g : 'glue'}

You have duplicate keys here, which becomes obvious when you spell out the 
values

testdict = {"filled": "apple", "filled": "banana", ...}

When you do that, the last value ("banana") wins, all others (e. g. "apple") 
are dropped.

> Now what I want to do, is if a variable is filled, print it out. This
> however isn't working how I planned. The following doesn't work.
> 
> for fillempt in testdict:
>     if fillempt == 'filled':
>         print(testdict[fillempt])
> 
> All this does though, is print glue, where I'd want it to print:
> 
> apple
> banana
> eggs
> glue
> 
> Perhaps a dictionary isn't the best way to do this.. I wonder what else I
> can do...

A dictionary is spot-on, but you have to use the unique "apple", 
"banana",... as keys:

>>> status = {"apple": "filled", "banana": "filled", "cake": "empty"}
>>> for item in status:
...     if status[item] == "filled":
...             print(item)
... 
apple
banana

Could it be that you just confused dict keys with dict values?

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


Thread

Why isn't this code working how I want it to? reubennottage@gmail.com - 2013-10-12 01:56 -0700
  Re: Why isn't this code working how I want it to? Marco Nawijn <nawijn@gmail.com> - 2013-10-12 02:17 -0700
  Re: Why isn't this code working how I want it to? Peter Otten <__peter__@web.de> - 2013-10-12 11:20 +0200
    Re: Why isn't this code working how I want it to? reubennottage@gmail.com - 2013-10-12 04:03 -0700
      Re: Why isn't this code working how I want it to? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-12 12:13 +0100
  Re: Why isn't this code working how I want it to? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-12 10:39 +0100
  Re: Why isn't this code working how I want it to? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-10-12 12:50 +0300

csiph-web