python - 从一串文本中获取三元组
问题描述
定义将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"))
预期输出:
解决方案
推荐阅读
- c++ - 如何在 C++ Lambda 中捕获向量中的元素?
- c# - C# WebSocket - 无法连接到 C# FleckWebSocket
- flutter - Webview 自动填充用户名和电子邮件字段自动填充会话值
- c++ - 比较函数导致向量排序的向量问题[leetcode]
- google-bigquery - 如何在视图或计划查询之间进行选择,以对通过 Stitch 导入的 BigQuery 表进行重复数据删除?
- css - 同一设备型号上的不同布局结果
- javascript - 如何在所有浏览器的画布上正确绘制视频缩略图?
- google-chrome - Chrome 84“打破”手风琴和“阅读更多”按钮
- c# - 操作无法完成,因为 DbContext 已在 Web API 中释放
- python - 如何将制表符分隔的文件读入具有不等长行的 Python?