【问题标题】:How to divide the string depending on length in seqeunce in python?如何在python中根据序列的长度划分字符串?
【发布时间】:2020-07-06 18:01:52
【问题描述】:
>>>'mymedicinesarerighthere'

你需要按以下方式分解这个词:

>>>['m', 'ym', 'edi', 'cine', 'sarer', 'ighthe', 're']

【问题讨论】:

  • 你试过什么。
  • 我尝试执行字符串切片。它没有工作

标签: python python-3.x string list slice


【解决方案1】:

这里是 python 生成器版本。

def gen(a):
    start=0
    step=1
    while a[start:start+step]:
        yield a[start:start+step]
        start+=step
        step+=1

list(gen('mymedicinesarerighthere'))
# ['m', 'ym', 'edi', 'cine', 'sarer', 'ighthe', 're']

这是另一种方法。

您可以观察到每个切片的起始索引是0,1,3,6,10...它们之间的差异nth em> 和 n-1th 在 AP 1,2,3,4,5...

要得到分区的数量,你必须解这个方程。

=>(n*(n+1))/2=len(string)
=>n^2+n-2*len(string)=0

使用math.ceil(n) 获取n 的ceil 值将给我们这里的分区数,当字符串的长度为23 时,它是7。

import math
def start(n):
    return n*(n+1)//2
def findRoots(c):
    a=b=1
    d = b * b - 4 * a * c 
    sqrt_val = math.sqrt(abs(d))
    return math.ceil((-b + sqrt_val)/(2 * a))
out=[s[start(i-1):start(i-1)+i] for i in range(1,findRoots(-2*len(s))+1)]
# ['m', 'ym', 'edi', 'cine', 'sarer', 'ighthe', 're']

【讨论】:

    【解决方案2】:
    s='mymedicinesarerighthere'
    res=[]
    i,x=0,1
    while (i+x)<len(s):
        res.append(s[i:x+i])
        i=i+x
        x=x+1
    res.append(s[i:len(s)])
    print(res)
    

    输出:

    ['m', 'ym', 'edi', 'cine', 'sarer', 'ighthe', 're']
    

    【讨论】:

      【解决方案3】:

      这是一个相当干净的解决方案,使用了一些 itertools:

      from itertools import count, islice
      
      def chunks(s):
          i = iter(s)
          for n in count(1):
              chunk = ''.join(islice(i, n))
              if not chunk:
                  return
              yield chunk
      
      >>> list(chunks("mymedicinesarerighthere"))
      ['m', 'ym', 'edi', 'cine', 'sarer', 'ighthe', 're']
      

      【讨论】:

      • 我尝试用纯 Python 方法编写您的 itertools 版本。好玩。不错的答案虽然+1。我知道切片在 AP(算术级数)中,即 0,1,3,6,10,15... 在我看到你的答案后我能够做到。如果您查看我的代码并提供反馈,那就太好了。非常感谢
      【解决方案4】:

      解决方法如下:

      tmpStr = "mymedicinesarerighthere"
      
      splitList = []
      
      ind = 1
      while True:
          if len(tmpStr) > ind:
              splitList.append(tmpStr[0:ind])
              tmpStr = tmpStr[ind:]
              ind += 1
          else:
              splitList.append(tmpStr)
              break
      
      print(splitList)
      
      >>> ['m', 'ym', 'edi', 'cine', 'sarer', 'ighthe', 're']
      

      【讨论】:

        【解决方案5】:

        代码

        txt = "mymedicinesarerighthere"
        
        i = 0
        k = 1
        myList = []
        while i+k < len(txt):
          myList.append(txt[i:i+k])
          i += k
          k += 1
        myList.append(txt[i: len(txt)])
        print(myList)
        

        输出

        ['m', 'ym', 'edi', 'cine', 'sarer', 'ighthe', 're']
        

        【讨论】:

        • @ArshiaMd 很高兴它对你有用。不要忘记将其标记为正确答案并点赞:)
        猜你喜欢
        • 1970-01-01
        • 2014-06-23
        • 1970-01-01
        • 2019-09-03
        • 2011-02-04
        • 2017-07-05
        • 2022-08-16
        • 1970-01-01
        相关资源
        最近更新 更多