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

R

reubennottage

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

Marco Nawijn

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,

and do a

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
 
P

Peter Otten

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:
.... if status[item] == "filled":
.... print(item)
....
apple
banana

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

Mark Lawrence

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
 
J

Jussi Piitulainen

[...] 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.
 
R

reubennottage

(e-mail address removed) 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:

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:



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

Mark Lawrence

(e-mail address removed) 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:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top