【问题标题】:Grouping together words in a string which have common substrings of some length将字符串中的单词组合在一起,这些单词具有一定长度的公共子字符串
【发布时间】:2021-03-10 12:30:57
【问题描述】:

我想在字符串中显示带有公共子字符串的单词。 例如,如果给定的字符串是

str = "the games are lame"

并且单词必须根据长度为3的公共子串组合在一起,所以输出应该是

the 
games, lame 
are

因为长度为 3 的公共子串是“ame”。

我继续使用 split() 将字符串转换为列表说“lista”,并让另一个列表说“listb”,其中包含所有可能的长度为 3 的子字符串,例如

the, gam, gme, ges, ame, aes, mes, are, lam, lme, ame

然后我检查了“listb”中的重复项目('ame'),并根据它们与“lista”中的项目进行比较

for items in duplicate:
       for item in lista:
           if items in item and not in listc:
               listc.append(item)

现在,我有一个“listc”,其中包含长度为 3 的公共子字符串的项目,但我不知道如何在输出中根据需要对它们进行分组。此外,如果“str”包含更多带有公共子字符串的单词,“listc”也将具有这些常见单词。 我不知道我是否应该以这种方式进行,并且似乎无法弄清楚如何根据输出中的需要对“listc”中的项目进行分组。

【问题讨论】:

    标签: python


    【解决方案1】:

    这是一个解决方案

    str_ = "the games are lame"
    
    # first I get a list of all the words
    words = str_.split()
    # words >>> ['the', 'games', 'are', 'lame']
    
    groups = []
    # This variable will contain the list of words
    
    # For each words
    for word in words:
        found = False
    
        # Get the first words of each groups
        other_words = [x[0] for x in groups if x != word]
    
        # Loop through the word and get all substring of 3 characters
        for i in range(len(word)):
            substring = word[i:i+3]
    
            # Eliminates the substring that doesn't have the correct length
            if len(substring) != 3:
                continue
    
            try:
                # try to find the substring in a group and get the corresponding index of that group
                index = [substring in other_word for other_word in other_words].index(True)
                found = True
    
                # Add the word in the group
                groups[index].append(word)
            except ValueError:
                continue
    
        # If we don't find a group for the word, we create a new group with that word in it
        if not found:
            groups.append([word])
    
    
    # groups >>> [['the'], ['games', 'lame'], ['are']]
    
    # Now print the groups
    for group in groups:
        print(", ".join(group))
    

    输出:

    the
    games, lame
    are
    

    【讨论】:

      【解决方案2】:

      我认为您在那里创建了很多列表,这可能会让人很困惑。

      如果您想使用纯逻辑方法而不使用专为序列匹配设计的库,例如difflib,您可以先定义一个比较两个字符串的函数;然后你将你的句子分成一个单词列表并通过该列表执行双重迭代(嵌套)比较所有可能的对。

      如果字符串匹配,它们将打印在以逗号分隔的同一行上,否则打印在新行上。

      在下面的函数中,我还为要匹配的子字符串的长度添加了一个参数,默认设置为 3 以符合您的问题:

      # This function compairs two strings and returns them in a tuple if they contain the 
      # same substring of len_substring characters.
      
      def string_matcher(string_a, string_b, len_substring = 3):
          for i in range(len(string_a)-len_substring):
              if string_a[i:i+len_substring] in string_b:
                  return string_a, string_b
          return None
      
      string = "the games are lame"
      words = string.split()
      
      output = ""
      
      # Making a double iteration over the words list and calling string_matcher for each pair.
      for i in range(len(words)-1):
          output = output+words[i]
          for j in range(i+1, len(words)):
              try:
                  word_a, word_b = string_matcher(words[i], words[j])
                  output = output+", "+word_b
              except TypeError:
                  pass
          output = output+"\n"
      
      print(output)
      

      程序打印出来:

      the
      games, lame
      are
      

      【讨论】:

        猜你喜欢
        • 2021-12-03
        • 2017-06-12
        • 1970-01-01
        • 1970-01-01
        • 2020-10-12
        • 1970-01-01
        • 1970-01-01
        • 2019-05-03
        • 1970-01-01
        相关资源
        最近更新 更多