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


Groups > comp.lang.python > #56734 > unrolled thread

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

Started byreubennottage@gmail.com
First post2013-10-12 01:56 -0700
Last post2013-10-12 12:50 +0300
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  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

#56734 — Why isn't this code working how I want it to?

Fromreubennottage@gmail.com
Date2013-10-12 01:56 -0700
SubjectWhy isn't this code working how I want it to?
Message-ID<f74fadac-e7c3-4419-9a65-ed6ecd5eb3fe@googlegroups.com>
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'}


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...

Thanks for any help.

[toc] | [next] | [standalone]


#56735

FromMarco Nawijn <nawijn@gmail.com>
Date2013-10-12 02:17 -0700
Message-ID<fb8f9915-c15b-4c8d-a85d-9bd24f69248e@googlegroups.com>
In reply to#56734
On Saturday, October 12, 2013 10:56:27 AM UTC+2, reuben...@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'}
> 
> 
> 
> 
> 
> 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...
> 
> 
> 
> Thanks for any help.

Hi,

Remember that keys in a dictionary are unique. So if you defined (>>> means it I typed it at the interactive terminal prompt,

>>> d = { 'filled' : 'apple' , 'filled' : 'orange' }

and do a 

>>> print d

it will show:
>>> 
{'filled': 'orange'}

One way to solve this problem is to define two dictionaries. 
One holding the status of the variable, the other one holding
the data. For example:

status = { 'a' : 'filled',  'b' : 'empty', 'c' : 'filled' }
data   = { 'a' : 'orange',  'b' : 'apple', 'c' : 'banana' }

for k in status:
    if status[k]=='filled':
        print data[k]

Regards and let us know if it works for you,

Marco

[toc] | [prev] | [next] | [standalone]


#56736

FromPeter Otten <__peter__@web.de>
Date2013-10-12 11:20 +0200
Message-ID<mailman.1032.1381569606.18130.python-list@python.org>
In reply to#56734
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?

[toc] | [prev] | [next] | [standalone]


#56740

Fromreubennottage@gmail.com
Date2013-10-12 04:03 -0700
Message-ID<50950ef3-9fae-41cd-8a9a-4bd3ccc418c6@googlegroups.com>
In reply to#56736
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. 

[toc] | [prev] | [next] | [standalone]


#56741

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-10-12 12:13 +0100
Message-ID<mailman.1035.1381576449.18130.python-list@python.org>
In reply to#56740
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

[toc] | [prev] | [next] | [standalone]


#56737

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-10-12 10:39 +0100
Message-ID<mailman.1033.1381570800.18130.python-list@python.org>
In reply to#56734
On 12/10/2013 09:56, 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'}
>
> 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...
>
> Thanks for any help.
>

You've effectively set up a dictionary with keys 'filled' and 'entries' 
which you can see if you run this loop

for key, value in testdict.items():
     print(key, value)

which gives me this

empty fish
filled glue

I'm too lazy to type anything else so please refer to this 
http://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists-in-python. 
  I'll also leave the argument over whether it's a variable or a name to 
others :)

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

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#56738

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-10-12 12:50 +0300
Message-ID<qotvc12q0mf.fsf@ruuvi.it.helsinki.fi>
In reply to#56734
reubennottage@gmail.com writes:

> [...] The following doesn't work.
> 
> for fillempt in testdict:
>     if fillempt == 'filled':
>         print(testdict[fillempt])

This is equivalent to

for fillempt in testdict:
   if fillempt == 'filled':
      print(testdict['filled'])

which in turn can be optimized to

if 'filled' in testdict:
   print(testdict['filled'])

without knowing anything of the contents of tesdict.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web