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


Groups > comp.lang.python > #56741

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; 'digest': 0.04; 'from:addr:yahoo.co.uk': 0.04; 'duplicate': 0.07; 'subject:code': 0.07; 'lawrence': 0.09; '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; 'worked.': 0.09; 'dict': 0.16; 'keys:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'red,': 0.16; 'right;': 0.16; 'slight': 0.16; 'values?': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'help.': 0.21; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'url:moin': 0.24; "i've": 0.25; 'post': 0.26; 'code:': 0.26; 'values': 0.27; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; 'wonder': 0.29; "doesn't": 0.30; 'work.': 0.31; 'url:wiki': 0.31; 'keys': 0.31; 'though.': 0.31; 'url:python': 0.33; '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; 'url:org': 0.36; 'thank': 0.38; 'apple': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'does': 0.39; '12,': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'read': 0.60; 'most': 0.60; 'tell': 0.60; 'simple': 0.61; "you've": 0.63; 'email addr:gmail.com': 0.63; 'soon': 0.63; 'great': 0.65; 'due': 0.66; 'status:': 0.68; 'obvious': 0.74; 'subject:this': 0.83; 'glance': 0.84; 'otten': 0.84; 'subject:want': 0.91; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Why isn't this code working how I want it to?
Date Sat, 12 Oct 2013 12:13:55 +0100
References <f74fadac-e7c3-4419-9a65-ed6ecd5eb3fe@googlegroups.com> <mailman.1032.1381569606.18130.python-list@python.org> <50950ef3-9fae-41cd-8a9a-4bd3ccc418c6@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-78-147-178-154.as13285.net
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.0.1
In-Reply-To <50950ef3-9fae-41cd-8a9a-4bd3ccc418c6@googlegroups.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 <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.1035.1381576449.18130.python-list@python.org> (permalink)
Lines 125
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1381576449 news.xs4all.nl 15946 [2001:888:2000:d::a6]:41556
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:56741

Show key headers only | View raw


On 12/10/2013 12:03, reubennottage@gmail.com wrote:
> On Saturday, October 12, 2013 10:20:24 AM UTC+1, Peter Otten wrote:
>> 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?
>
> This fixed it, thank you! I did think a dictionary was right; I never considered swapping the keys with the values, though. A simple 'fix, but it worked. You've been a great help.
>

That's good to hear.

Would you please read and digest this 
https://wiki.python.org/moin/GoogleGroupsPython if you need to post 
again, a quick glance above will soon tell you why :)

-- 
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence

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