python-返回以str作为键的dict对象,并将其作为值出现的次数

python-返回以str作为键的dict对象,并将其作为值出现的次数,python,Python,传递一个 作为参数的文本字符串。函数首先转换参数 字符串,然后返回具有以下内容的dictionary对象: remove_less_than_2代码删除结果字典中的任何对,其中 最后三个字母在文本的参数字符串中只出现一次 参数中任何单词的最后三个字母的键 长度大于2的文本字符串,以及 对应的值,即参数中的字数 以最后三个字母结尾的文本字符串 到目前为止,我有: sentence = 'west best worst first tapping snapping in a pest the s

传递一个 作为参数的文本字符串。函数首先转换参数 字符串,然后返回具有以下内容的dictionary对象:

  • remove_less_than_2
    代码删除结果字典中的任何对,其中 最后三个字母在文本的参数字符串中只出现一次

  • 参数中任何单词的最后三个字母的键 长度大于2的文本字符串,以及

  • 对应的值,即参数中的字数 以最后三个字母结尾的文本字符串

到目前为止,我有:

sentence = 'west best worst first tapping snapping in a pest the straining singing forest living'

def remove_less_than_2(a_dict):

    all_keys = list(a_dict.keys())
    for key in all_keys:
      if a_dict[key] == 1:
         del a_dict[key]

def get_last_three_letters_dict(sentence):

    new_dict = {}

    new_sentence = sentence.lower()
    new_sentence = sentence.split()
    for word in new_sentence:
        if len(word) > 2:
            new_dict[word[-3:]] = sentence.count(word[-3:])
    return new_dict
但它返回的值超出了它应该返回的值

est : 4
ing : 6
rst : 2
我做错了什么

sentence = ' "west best worst first tapping snapping in a pest the straining singing forest living'

# Get the last 3 letters of each word if its length is greater than 3
words_gt3 = [word[-3:] for word in sentence.split() if len(word) >= 3]

# Count them (you can use collections.Counter() too)
out = {}
for w in words_gt3:
    if w not in out.keys():
        out[w] = 0
    out[w] += 1

# Filter non repeated words
out = dict([e for e in out.items() if e[1] > 1])
print out
# {'rst': 2, 'est': 4, 'ing': 5}
我做错了什么

sentence = ' "west best worst first tapping snapping in a pest the straining singing forest living'

# Get the last 3 letters of each word if its length is greater than 3
words_gt3 = [word[-3:] for word in sentence.split() if len(word) >= 3]

# Count them (you can use collections.Counter() too)
out = {}
for w in words_gt3:
    if w not in out.keys():
        out[w] = 0
    out[w] += 1

# Filter non repeated words
out = dict([e for e in out.items() if e[1] > 1])
print out
# {'rst': 2, 'est': 4, 'ing': 5}
句子.count()
:统计整个字符串(而不是每个单词)中出现的次数。 所以
'singing'.count('ing')
将返回
2
。这就是为什么您计算的是
6
ing
而不是
5

我做错了什么

sentence = ' "west best worst first tapping snapping in a pest the straining singing forest living'

# Get the last 3 letters of each word if its length is greater than 3
words_gt3 = [word[-3:] for word in sentence.split() if len(word) >= 3]

# Count them (you can use collections.Counter() too)
out = {}
for w in words_gt3:
    if w not in out.keys():
        out[w] = 0
    out[w] += 1

# Filter non repeated words
out = dict([e for e in out.items() if e[1] > 1])
print out
# {'rst': 2, 'est': 4, 'ing': 5}
句子.count()
:统计整个字符串(而不是每个单词)中出现的次数。
所以
'singing'.count('ing')
将返回
2
。这就是为什么您计算的是
6
ing
而不是
5

您试图通过听写/列表理解和计数器轻松实现的目标:

from collections import Counter

sentence = ' "west best worst first tapping snapping in a pest the straining singing forest living'

filtered_counter = {k: v for k, v in Counter([word[-3:] for word in sentence.lower().split() if len(word) > 2]).items() if v > 1}
首先,我们从标准库导入
计数器
类型,并定义
语句
。下一行是在降低整个句子并检查单词长度是否至少为3后,创建一个包含每个单词最后3个字母的数组;它正在从中创建一个计数器对象,它生成一个类似于dict的
对象,该对象枚举在数组中找到一个元素的次数;然后使用听写理解过滤输出,使其不包含不重复的单词

我将分解这一行,以便您能更好地看到它:

lowered_sentence = sentence.lower()
words = lowered_sentence.split()
filtered_words = [word for word in words if len(word) > 2]
# filtered_words = filter(lambda x: len(x) > 2, words)
word_ends = [word[-3:] for word in filtered_words]
counter = Counter(word_ends)
filtered_counter = {key: value for key, value in counter.items() if value > 1}

您试图通过dict/list理解和计数器轻松实现的目标:

from collections import Counter

sentence = ' "west best worst first tapping snapping in a pest the straining singing forest living'

filtered_counter = {k: v for k, v in Counter([word[-3:] for word in sentence.lower().split() if len(word) > 2]).items() if v > 1}
首先,我们从标准库导入
计数器
类型,并定义
语句
。下一行是在降低整个句子并检查单词长度是否至少为3后,创建一个包含每个单词最后3个字母的数组;它正在从中创建一个计数器对象,它生成一个类似于dict的
对象,该对象枚举在数组中找到一个元素的次数;然后使用听写理解过滤输出,使其不包含不重复的单词

我将分解这一行,以便您能更好地看到它:

lowered_sentence = sentence.lower()
words = lowered_sentence.split()
filtered_words = [word for word in words if len(word) > 2]
# filtered_words = filter(lambda x: len(x) > 2, words)
word_ends = [word[-3:] for word in filtered_words]
counter = Counter(word_ends)
filtered_counter = {key: value for key, value in counter.items() if value > 1}

你没有像他问的那样过滤不重复的单词。直截了当的听写理解应该可以做到这一点。正如我在回答中建议的那样,我仍然会使用
计数器来避免循环,但你的答案也是正确的。列表理解和循环仍然比
计数器快,但你可以自由使用你想要的。我仍然使用列表理解和听写理解。这种应用的速度差完全可以忽略不计。如果您需要优化Python脚本以用于循环,而不是标准库中的容器,那么您应该考虑重构代码,甚至切换到编译语言。直截了当的听写理解应该可以做到这一点。正如我在回答中建议的那样,我仍然会使用
计数器来避免循环,但你的答案也是正确的。列表理解和循环仍然比
计数器快,但你可以自由使用你想要的。我仍然使用列表理解和听写理解。这种应用的速度差完全可以忽略不计。如果您需要优化Python脚本以用于循环,而不是从标准库中使用容器,那么您应该考虑重构代码,甚至切换到编译语言。