【发布时间】:2021-05-06 13:44:18
【问题描述】:
没有看到有人在不使用正则表达式的情况下解决类似的问题。所以我有一个文本 = “我有三个苹果,因为昨天我买了三个苹果”和关键词列表:单词 = ['我有','三个','苹果','昨天'] 和 k = 2。我写了一个函数来查找并返回 >= k 'words' 的单词序列(单词是指识别为列表中单个元素的单词组合,例如'I have'被认为是一个单词)。
在这种情况下,它应该返回['我有三个苹果','三个苹果']。即使 'yesterday' 在字符串中,它是 我假设需要有一个堆栈来跟踪序列的大小。我开始编写代码时没有 1) 它在这种情况下不起作用,因为我尝试检查“我有”,然后“我有三个”等,但它不能只识别“三个苹果”; 2)我不知道如何进行。代码如下: 非常感谢解决此问题的不同方法或任何想法,因为我一直在尝试以这种方式解决它并且我不知道如何实现它。text = "I have three apples because yesterday I bought three apples"
words = ['I have', 'three', 'apples', 'yesterday']
k = 2
check1 = []
def search(text, words, k):
for i in words:
finding = text.count(i)
if finding != 0:
check1.append(i)
check2 = ' '.join(check1)
occurrences = text.count(check2)
if occurrences > 0:
#i want to check if the previous number of occurrences was the same
#that's why I think I need a stack. if it is, i keep going
#if it's not, i append the previous phrases to the list if they're >= k and keep
#checking
pass
else:
#the next word doesn't belong to the sequence, so we finish the process
#by adding the right number of word sequences >= k to the resulting list
pass
else:
#the word is not in the list and I need to add check2 to the list
#considering all word sequences
pass
【问题讨论】:
-
顺序重要吗?例如,“我有多少苹果?”的结果应该是什么? ?
-
@PM77-1 是的,不幸的是,确实如此。所以它只能是“三个苹果”,而不是“三个苹果”。您的示例的结果将是一个空列表,因为只有 'apples' 和 'I have' 小于 k
-
对不起。这是一个更好的例子,“我只有三个苹果吗?”。
-
@PM77-1 在这里它会返回 ['三个苹果']。如果它是“我只有三个苹果是三个”,那么它将返回 ['三个苹果','我有三个']
-
您可以将单词列表转换为有向边的图形,指示允许跟随的单词。然后,您只需根据输入字符串遍历此图并记录您走过的允许路径。
标签: python python-3.x string list