mb1
mb1

Reputation: 21

dictionary text start and end with same letter

Write a function named symmetry that takes a string, text, as a parameter and returns a dictionary d. Every letter that is both the first and last letter of some word in text should be a key in d. For example, if text contains the word 'shucks', 's' should be a key in d. The value of 's' in d is the number of words that both begin and end with 's'. The parameter text contains only upper and lower case characters and white space.

The following is an example of correct output:

t = '''The sun did not shine
it was too wet to play
so we sat in the house
all that cold cold wet day

I sat there with Sally
we sat there we two
and I said how I wish
we had something to do'''

print(symmetry(t))
{'d': 1, 'i': 3, 't': 1}

I have tried multiple times to get an answer to this question but I cannot seem to get it. With my code, every single letter is put into the dictionary instead of only letters that start and end a word. Can you please help me better understand this?

This is my code so far:

def symmetry(text):
   d = {}
   text.split()
   for word in text:
      if word[0] == word[-1]:
         d[word[0]] =+ 1
      else:
   return word
return d

text = ''' The sun did not shine
it was too wet to play
so we sat in the house
all that cold cold wet day

I sat there with Sally
we sat there we two
and I said how I wish
we had something to do'''

print(symmetry(text))

Upvotes: 2

Views: 1525

Answers (5)

Trey Hunner
Trey Hunner

Reputation: 11814

The most idiomatic solution would be to use Counter from the collections module.

from collections import Counter

def symmetry(text):
    return Counter(w[0] for w in text.lower().split() if w[0] == w[-1])

If you really need a dictionary and not a dictionary-like object (Counter inherits from dict actually), you can do this:

def symmetry(text):
    return dict(Counter(w[0] for w in text.lower().split() if w[0] == w[-1]))

Related article I wrote comparing the different methods for counting things

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174816

You're almost there. First define a default dictionary with the first letter as key and 0 as value. Then iterate over the split list and append the item to the dictionary defined only if the first letter and the last letter of the item is same. Finally use a dict comprehension to filter the dict items which has the value greater than 0.

text = ''' The sun did not shine
    it was too wet to play
    so we sat in the house
    all that cold cold wet day

    I sat there with Sally
    we sat there we two
    and I said how I wish
    we had something to do'''
def symmetry(t):
    j = t.split()
    d = {}
    for i in  j:
        if i[0] == i[-1]:
            d[i[0]] = d.get(i[0], 0) + 1
    return {i:j for i,j in d.items() if j > 0}

>>> print symmetry(text)
{'I': 3, 'd': 1, 't': 1}
>>> 

Upvotes: 0

Bug Hunter 219
Bug Hunter 219

Reputation: 344

              def symmetry(text):
               d = {}
               list = text.split(" ")
               for word in list:
                  word = word.strip()
                  if word and word[0] == word[len(word)-1]:
                     try:
                        d[word[0]] = d[word[0]] + 1
                     except:
                        d[word[0]] = 1
               return d

            text = ''' The sun did not shine
            it was too wet to play
            so we sat in the house
            all that cold cold wet day 

            I sat there with Sally
            we sat there we two
            and I said how I wish
            we had something to do'''

            print(symmetry(text))

Upvotes: 0

Antimony
Antimony

Reputation: 2240

You need to store the list of words you get, after you call split() on text. Right now, it doesn't save that list, and so word actually takes on the value of each letter in text.

Upvotes: 1

Tomasz Kaminski
Tomasz Kaminski

Reputation: 910

from collections import defaultdict
result = defaultdict(lambda: 0)
for letter in [word[0] for word in t.split() if word[0] == word[-1]]:
    result[letter] += 1

Should give you the idea:

Now, you need to change the if condition, so that it works for both lower-, and uppercase letters.

Read about list comprehensions, which will explain how expressions like:

[word[0] for word in t.split() if word[0] == word[-1]]

can be formed, and what do they mean.

Upvotes: 2

Related Questions