compare dictionaries

B

Baba

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
 
P

Paul Rubin

Baba said:
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())
 
G

Gary Herron

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
 
B

Baba

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

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
 
M

MRAB

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

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

Peter Otten

Baba said:
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

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
 
B

Baba

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.

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
 
P

Paul Rubin

Baba said:
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)
 
G

Gary Herron

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

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

MRAB

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.

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

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top