Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

compare dictionaries

0 views
Skip to first unread message

Baba

unread,
Sep 7, 2010, 3:46:36 PM9/7/10
to
level: beginner

word= 'even'
dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2}

i want to know if word is entirely composed of letters in dict2

my approach:
step 1 : convert word to dictionary(dict1)

step2:
for k in dict1.keys():
if k in dict2:
if dict1[k] != dict2[k]:
return False
return True
return False
return True


by adding a print statement i can see that this simply ends too early
e.g. as soon as the first IF condition is met the loop exits

i think this is easy but google and python doc didn't return any good
hints so i'm trying here.


Thanks Baba

Paul Rubin

unread,
Sep 7, 2010, 4:06:01 PM9/7/10
to
Baba <raou...@gmail.com> writes:
> word= 'even'
> dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2}
>
> i want to know if word is entirely composed of letters in dict2

set(word) <= set(dict2.keys())

Gary Herron

unread,
Sep 7, 2010, 4:08:27 PM9/7/10
to pytho...@python.org
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

Baba

unread,
Sep 7, 2010, 4:26:10 PM9/7/10
to

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

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

MRAB

unread,
Sep 7, 2010, 4:37:58 PM9/7/10
to Python List

Do the numbers in dict2 represent the maximum number of times that the
letter can be used?

If yes, then build a similar dict for the word with the number of times
that each letter occurs in the word and then check for every pair in
the dict whether the key (ie, letter) occurs in dict2 and that the
value (number of occurrences) isn't too many.

Peter Otten

unread,
Sep 7, 2010, 5:08:49 PM9/7/10
to
Baba wrote:

Just compare the two dictionaries

dict1 == dict2

Or, if you want to allow dict2 to contain higher but not lower values

all(v <= dict2.get(k, 0) for k, v in dict1.iteritems())

Peter

Baba

unread,
Sep 7, 2010, 5:36:38 PM9/7/10
to
On 7 sep, 22:37, MRAB <pyt...@mrabarnett.plus.com> wrote:
> On 07/09/2010 21:06, Paul Rubin wrote:
>
> > Baba<raoul...@gmail.com>  writes:

Hi MRAB

Thanks for the hint. In my case i need to do the opposite: the number
of times that each letter ocurs in the word needs to be smaller or
equal to the number of times it apears in dict2. That way i am
guaranteed that word is entirely made up of elements of dict2.

Your hint pointed me in the right direction.

for k in word.keys():
if k not in hand:
return False
elif k in hand:
if word[k] > hand[k]:
return False
return True

Baba

Paul Rubin

unread,
Sep 7, 2010, 5:56:07 PM9/7/10
to
Baba <raou...@gmail.com> writes:
> for k in word.keys():
> if k not in hand:
> return False
> elif k in hand:
> if word[k] > hand[k]:
> return False
> return True

Untested:

all(word[k] <= hand.get(k,0) for k in word)

Gary Herron

unread,
Sep 7, 2010, 6:49:35 PM9/7/10
to pytho...@python.org

Huh??? I answered the problem as it was stated in the email -- it said
nothing about *counting* the occurrences of letters. In order to not
waste our (voluntary) time, perhaps you should carefully re-state the
problem you'd liked solved. Then we'll see what we can come up with.


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

MRAB

unread,
Sep 7, 2010, 7:17:26 PM9/7/10
to Python List
If the first condition is True then the second will be False, so
there's no need to check it:

for k in word.keys():
if k not in hand:
return False

else:


if word[k] > hand[k]:
return False
return True

This can be shortened still further.

pdle...@earthlink.net

unread,
Sep 8, 2010, 1:09:59 AM9/8/10
to
On Tue, 7 Sep 2010 12:46:36 -0700 (PDT), Baba <raou...@gmail.com>
wrote:

>level: beginner
>
>word= 'even'
>dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2}
>
>i want to know if word is entirely composed of letters in dict2
>
>my approach:
>step 1 : convert word to dictionary(dict1)
>
>step2:
>for k in dict1.keys():
> if k in dict2:
> if dict1[k] != dict2[k]:
> return False
> return True
> return False
> return True
>

Assign letters to their corresponding primes with the
following function -

def alphaprime(c) :
if c == 'a' return 2
elif c == 'b' return 3
elif c == 'c' return 5
...
elif c == 'y' return 97
elif c == 'z' return 101
else : return 0

Using above calculate a composite for the letters in the
dictionary. Of course begin with 1 and multiply by the
associated prime for each letter and repeat for each
recurrence of that letter. Call this dictionarycomposite.

Do the same for the word, parsing sequentially and
multiplying by the prime for each letter. Call this
wordcomposite.

Now if
dictionarycomposite % wordcomposite == 0
the word can be spelled with the letters in the dictionary.

I used this in a word game : works fast.
The Fundamental Theorum of Arithmetic may apply.
Dave WB3DWE

0 new messages