定义传递一串文本的get_triples_dict()
函数
作为参数。该函数首先将参数字符串转换为
小写,然后返回一个包含所有键的字典
独特的连续三个字母字符来自文本,以及
对应的值是连续三次的次数
字母字符出现在文本中。使用isalpha()
方法
检查一个字符是否是字母。字典应该只
包含多次出现的条目。你的字典后
已创建并填充,您需要删除任何键值对
具有相应的值1。
有人可以帮我修改我的代码,以便我能获得正确的输出吗?我试过了,但它并没有完全奏效。我只需要get_triples_dict
函数的帮助,我不需要触及test_get_triples_dict
函数。以下是我到目前为止的预期输出:
def get_triples_dict(text):
triples_dict = {}
word = ""
mod_text = ""
# convert the text to contain only letters and convert all letters to lowercase
for i in range(0,len(text)):
if text[i].isalpha():
mod_text = mod_text + text[i].lower()
# if length of text >= 3 letters
if(len(mod_text) >= 3):
# loop over the text
for i in range(0,len(mod_text)-2):
word = mod_text[i:i+3] # extract 3 letter word from starting with the letter at i
if word in triples_dict.keys(): # if word is in dictionary then increment its frequency
triples_dict[word] = triples_dict[word] + 1
else: # else insert the word in dictionary with frequency 1
triples_dict[word] = 1
keys = triples_dict.keys() # get the list of keys in the dictionary
# loop over the keys list
for i in range(0,len(keys)):
if triples_dict[keys] == 1: #if frequency of the word is 1, then remove the entry from dictionary
del triples_dict[keys]
i = i - 1 # decrement i
return triples_dict # return the dictionary
def test_get_triples_dict():
print("1.")
print_dict_in_key_order(get_triples_dict('super, duper'))
print("\n2.")
print_dict_in_key_order(get_triples_dict("ABC ABC ABC"))
print("\n3.")
print_dict_in_key_order(get_triples_dict("Sometimes the smallest things make more room in your heart"))
print("\n4.")
print_dict_in_key_order(get_triples_dict("My favourite painting is the painting i did of my dog in that painting in my den"))
预期输出: