【问题标题】:dictionary text start and end with same letter字典文本以相同的字母开头和结尾
【发布时间】:2016-02-19 14:16:43
【问题描述】:

编写一个名为symmetry 的函数,它接受一个字符串文本作为参数 并返回字典 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))

【问题讨论】:

  • d[word[0]] =+ 1 - =+ 不是运算符。它被解释为= +1,或者实际上只是= 1。我猜这不是本意?

标签: python dictionary python-3.4


【解决方案1】:
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]]

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

【讨论】:

  • 非常感谢!!我一定会去看看不同的列表理解。这对您很有帮助。
【解决方案2】:

text 上调用split() 后,您需要存储获得的单词列表。现在,它不保存该列表,因此 word 实际上采用文本中每个字母的值。

【讨论】:

  • 没问题,很高兴能帮上忙 :)
【解决方案3】:
              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))

【讨论】:

  • 非常感谢,帮了大忙!!
  • 使用 dicts 的get 方法会少一些麻烦:d[word[0]] = d.get(word[0], 0) + 1。或者您可以使用defaultdict
【解决方案4】:

你快到了。首先定义一个默认字典,第一个字母为键,0 为值。然后遍历拆分列表并将项目附加到仅当项目的第一个字母和最后一个字母相同时定义的字典。最后使用字典推导过滤值大于 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}
>>> 

【讨论】:

【解决方案5】:

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

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

【讨论】:

    猜你喜欢
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2020-12-28
    • 1970-01-01
    • 2018-10-29
    相关资源
    最近更新 更多