compare dictionaries

Shashwat Anand anand.shashwat at gmail.com
Tue Sep 7 16:42:11 EDT 2010


On Wed, Sep 8, 2010 at 1:56 AM, Baba <raoulbia at gmail.com> wrote:

> On 7 sep, 22:08, Gary Herron <gher... at digipen.edu> wrote:
> > On 09/07/2010 12:46 PM, Baba wrote:
> >
> > > word= 'even'
> > > dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2}
> >
> > Just go through each letter of word checking for its existence in
> > dict2.  Return False if one misses, and True if you get through the
> > whole word:
> >
> > def ...():
> >    for c in word:
> >      if c not in dict2:
> >        return False #if any character is not in dict
> >    return True      # otherwise
> >
> > If you know of generator expressions, and remember that True and False
> > are 1 and 0 respectively, then this works
> >
> > def ...():
> >      return sum(c in dict2   for c in word) == len(word)
> >
> > Gary Herron
> >
> > --
> > Gary Herron, PhD.
> > Department of Computer Science
> > DigiPen Institute of Technology
> > (425) 895-4418
>
> ok but how do we address the fact that letter e needs to have the
> value 2 in the dictionary if it was to be True? in my example this
> condition is not met so the check would return False. Word is not
> entirely composed of letters in dict2, one of the letter is not in
> dict2 i.e. the 2nd e
>

Seems You did not understood Gary's solution. It is correct. Also, you asked
for
'i want to know if word is entirely composed of letters in dict2'  which you
are getting.
Did you tried running the code ?


> So finding a matching key seems to be the easy part, checking if the
> number of ocurrences of letter in 'word' == letter.value seems to be
> the tricky part
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
~l0nwlf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100908/57d482be/attachment.html>


More information about the Python-list mailing list