字典文本以相同的字母开头和结尾

时间:2015-11-18 01:01:11

标签: python dictionary python-3.4

编写一个名为symmetry的函数,它将字符串text作为参数 并返回字典d。每个字母都是文本中某个单词的第一个和最后一个字母,应该是d中的一个键。例如,如果文本包含单词'shucks',则's'应该是d中的键。 d中's'的值是以's'开头和结尾的单词数。参数文本仅包含大小写字符和空格。

以下是正确输出的示例:

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}

我已经多次尝试过这个问题的答案,但我似乎无法得到它。使用我的代码,每个字母都放入字典中,而不是仅用于开始和结束单词的字母。能帮助我更好地理解这个吗?

到目前为止,这是我的代码:

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

5 个答案:

答案 0 :(得分:2)

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

应该给你一个想法:

现在,您需要更改if条件,以便它适用于小写和大写字母。

了解列表推导,它将解释表达式如何:

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

可以形成,它们是什么意思。

答案 1 :(得分:1)

split()上致电text后,您需要存储所获得的字词列表。现在,它并没有保存该列表,因此word实际上采用了文本中每个字母的值。

答案 2 :(得分:0)

你快到了。首先定义一个默认字典,第一个字母为键,0为值。然后迭代拆分列表,只有当项目的第一个字母和最后一个字母相同时,才将项目附加到定义的字典中。最后使用dict理解来过滤值大于0的dict项。

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

最常用的解决方案是使用Counter模块中的collections

from collections import Counter

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

如果你真的需要一个字典而不是类字典对象(Counter实际上是从dict继承),你可以这样做:

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