【问题标题】:How to get every possible list of substrings from a string in Python?如何从 Python 中的字符串中获取所有可能的子字符串列表?
【发布时间】:2018-07-21 21:47:09
【问题描述】:

我正在尝试将给定的字符串(“hello”)转换为包含每个子字符串列表的列表。例如:

[["hello"],["h,"ello"],["he","llo"],["hel","lo"],["hell","o"],\
["h","e","llo"],["h","e","l","lo"],["h","e","l","l","o],["he","l,"lo"],\
["hel","l,"o"],["hell","o]....etc....].

我知道最快的方法应该是递归函数,但我就是做错了。 类似于:

x = "hello"
wordset=[]
string_div(0,x,wordset)
...
...
def string_div(i,word,wordset)
  wordset.append(wordset+[word[i:])
  ......
  
(这与之前发布的其他问题不同,因为我只想要连接形成相同原始单词的子字符串列表) 帮助将不胜感激! 谢谢

【问题讨论】:

标签: python string list if-statement substring


【解决方案1】:

我相信这不是严格意义上的重复,我为您的问题提供了一个准确的解决方案。

解决方案

对于给定的长度为 n 的字符串,我们将为每个长度为 (n-1) 的二进制字符串获得唯一且有效的字符串分区。例如:字符串“coconut”和二进制字符串“001010”对应分区:['coc','on','ut']和二进制字符串“100101”对应:['c','oco ','nu','t'].

因此,我们可以通过取所有 ((2^(n-1))-1) 个不同的分区对应于不同的二进制序列来获得所需的完整分区列表。

实施

import itertools
def get_list_partitions(string):
  partitions = []
  
  #list of binary sequences
  binary_sequences = ["".join(seq) for seq in itertools.product("01", repeat=len(string)-1)]

  #go over every binary sequence (which represents a partition)
  for sequence in binary_sequences:
    partition = []
    
    #current substring, accumulates letters until it encounters "1" in the binary sequence
    curr_substring = string[0]
    for i, bit in enumerate(sequence):
      #if 0, don't partition. otherwise, add curr_substring to the current partition and set curr_substring to be the next letter
      if bit == '0':                        
        curr_substring = curr_substring + string[i+1]
      else:
        partition.append(curr_substring)
        curr_substring = string[i+1]  
    
    #add the last substring to the partition
    partition.append(curr_substring)

    #add partition to the list of partitions
    partitions.append(partition)  

  return partitions 

  
print(get_list_partitions("coconut"))
      

【讨论】:

    猜你喜欢
    • 2015-06-01
    • 2020-12-16
    • 2021-06-07
    • 2012-01-12
    • 2020-09-11
    • 2014-04-23
    • 2021-08-25
    • 1970-01-01
    • 2019-09-10
    相关资源
    最近更新 更多