issues with searching through dictionaries for certain values
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: issues with searching through dictionaries for certain values
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.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: issues with searching through dictionaries for certain values
Connolly [EMAIL PROTECTED] 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 -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/ 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: issues with searching through dictionaries for certain values
On Feb 1, 6:27 am, Connolly [EMAIL PROTECTED] 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) -- http://mail.python.org/mailman/listinfo/python-list