繁体   English   中英

查找文本中所有重复的单词

[英]Find all duplicates words in text

我试图在文本中查找所有重复的单词,每个重复的单词都包含在一个元组中,并将所有元组保存在一个列表中。 它需要在“so,so”之类的词之间用标点符号来整理案例

我尝试使用该模式:

/(\b\S+\b)\s+\b\1\b/

但它不会返回我正在寻找的内容,并且无法以我需要的形式保存结果

我正在寻找的示例:

the text = "i went to to a party, party at my uncle's house"

Output 末尾的 function:

[(to ,to), (party, party)]

正则表达式用于查找特定模式而不是单词,您应该做的是 @thshea 所说的,或者您可以使用以下代码:

_answer_ = []
the_text = "i went to to a party, party at my uncle's house"
the_text = the_text.replace(",","")
words = the_text.split(" ")
words2 = list(set(words))
for word in list(words2):
  if word in words:
    words.remove(word)
for word2 in words:
  _answer_ += [tuple([word2,word2])]
_answer_

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM