[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

issues with searching through dictionaries for certain values

Connolly

2/1/2008 2:28:00 PM

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



4 Answers

Zentrader

2/1/2008 3:28:00 PM

0

On Feb 1, 6:27 am, "Connolly" <cho.hat...@gmail.com> wrote:
> 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)

Steve Holden

2/1/2008 3:33:00 PM

0

Connolly wrote:
> 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
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...

Sion Arrowsmith

2/1/2008 3:35:00 PM

0

Connolly <cho.hatred@gmail.com> wrote:
>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

--
\S -- siona@chiark.greenend.org.uk -- http://www.chaos.org...
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

Phisher KIng

5/18/2008 8:45:00 PM

0

"Harry Dope" <Pres.McCain08@earthlink.com> wrote in news:4830799e$0$4099
$4c368faf@roadrunner.com:

> At least he doesnt support terrorists
>
>

The Burmese Junta are terrorist against their own people, moron.