issues with searching through dictionaries for certain values

C

Connolly

Hey,

Right basically I've got to the end of my main section of my program an I've
got it comparing the same dictionary to ensure that the values are the same
(sounds stupid I know), yet what my line of code I am using to do this is
failing to do is to check every single value. It is only checking the final
value in my dictionary, if you require more info than this feel free to say
but any help would be appreciated.
Code in use is displayed below.

f4 = files_stored[0]
f5 = files_stored[0]

for key in f4:
for items in f5:
if key == items:
f6 = {start+1: key}
final_dict.update(f6)
else:
print key, 'false'

print final_dict
 
Z

Zentrader

Hey,

Right basically I've got to the end of my main section of my program an I've
got it comparing the same dictionary to ensure that the values are the same
(sounds stupid I know), yet what my line of code I am using to do this is
failing to do is to check every single value. It is only checking the final
value in my dictionary, if you require more info than this feel free to say
but any help would be appreciated.
Code in use is displayed below.

f4 = files_stored[0]
f5 = files_stored[0]

for key in f4:
    for items in f5:
        if key == items:
            f6 = {start+1: key}
            final_dict.update(f6)
        else:
            print key, 'false'

print final_dict

This is the same thing that I think you want to do. Also, you never
increment the start variable so it always remains the same (i.e.
dictionary will only have one key), and the f6 dictionary will only
contain the last test anyway since you create a new dictionary on each
pass instead of adding to an existing dictionary. Don't know if that
is intentional or not.
set_f4 = set(f4.keys())
set_f5 = set(f5.keys())
print set_f4.difference(set_f5)
 
S

Steve Holden

Connolly said:
Hey,

Right basically I've got to the end of my main section of my program an I've
got it comparing the same dictionary to ensure that the values are the same
(sounds stupid I know), yet what my line of code I am using to do this is
failing to do is to check every single value. It is only checking the final
value in my dictionary, if you require more info than this feel free to say
but any help would be appreciated.
Code in use is displayed below.

f4 = files_stored[0]
f5 = files_stored[0]

for key in f4:
for items in f5:
if key == items:
f6 = {start+1: key}
final_dict.update(f6)
else:
print key, 'false'

print final_dict
Some points.

1: f4 and f5 are both references to the *same* dictionary, as you can
see by comparing id(f4) and id(f5).

2: Suppose you *did* have different dictionaries, if you want to test
that they have the same values associated with the same keys this
condition is known as "equality", and can be tested for with

f4 == f5

3: You seem to believe that you are iterating over the keys of f4 and
the items of f5, or perhaps you are just bad at choosing variable names.

4: The best way to write

f6 = {start+1: key}
final_dict.update(f6)

is normally

final_dict[start+1] = key

5: I presume start is set somewhere before this code, avoiding the
NameError or AttributeError. It's hard to see whether you want the same
value for all uses of start, but if you don't then you might want to
consider incrementing start after you use it.

6: What do you *really* want to do?

regards
Steve
 
S

Sion Arrowsmith

Connolly said:
Right basically I've got to the end of my main section of my program an I've
got it comparing the same dictionary to ensure that the values are the same
(sounds stupid I know), yet what my line of code I am using to do this is
failing to do is to check every single value. It is only checking the final
value in my dictionary [ ... ]

On what are you basing the assumption that it's only checking the
final value? The outputted final_dict? Have you tried putting a
"print final_dict" after the final_dict.update() call to see how
many times the check is actually made? And while your doing that,
you might like to stick in a "print f6" at the same place, and see
how many different values are being added to final_dict.

(I assume there is good reason which has been cut out for
f6 = {start+1: key}
final_dict.update(f6)
rather than
final_dict[start+1] = key
)
f4 = files_stored[0]
f5 = files_stored[0]

for key in f4:
for items in f5:
if key == items:
f6 = {start+1: key}
final_dict.update(f6)
else:
print key, 'false'

print final_dict
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top