【问题标题】:How to split the string in python and repeat it and join如何在python中拆分字符串并重复并加入
【发布时间】:2019-08-09 13:32:40
【问题描述】:

我的代码如下。我想计算输出中的字母

s = 字符串,n 是重复的次数。 n = 10,表示字符串 s = "aba" 在 10 个字母的过程中重复。即abaabaabaa。

s, n = input().strip(), int(input().strip())
print(s.count("a")  

输出 = 7

我的代码在下面

a = 'aba'
t = list(a)
n = 3
new_list = []
if n  <=  len(t):
    for i in range(n):
        new_list.append(t[i])
        m = t + new_list
    print (''.join(m))
elif n > len(t):
    x,y = divmod(n,len(t))
    if y == 0:
        new_list.append(a * x)
    else:
        new_list.append((a * x) + ''.join(map(str,t[:y])))

如果 n 很大,则需要像 len(list(s)) = 3 一样循环,如果 n = 10,除以 10/3 得到 3 等分,得到 1 个余数

【问题讨论】:

    标签: python list


    【解决方案1】:

    如果最终意图是迭代生成的字符串而不是直接将其打印为输出,则另一种方法是使用 itertools 库。

    from itertools import cycle, islice
    
    s = "aba"
    n = 10
    for c in islice(cycle(s), n):
        ... do other stuff ...
    

    另一种情况是您尝试将生成的字符串与另一个长度为n 的字符串s2 进行比较。在这种情况下,islice 可以省略。

    for c, c2 in zip(cycle(s), s2):  # zip() truncates cycle(s) to the same length as s2
         ... operate on c and c2 ...
    

    【讨论】:

      【解决方案2】:

      你可以这样使用:

      def repeat_n(s, n):
        return (s * (n // len(s) + 1))[:n]
      
      s = 'aba'
      print(repeat_n(s, 2))
      # ab
      
      print(repeat_n(s, 10))
      # abaabaabaa
      
      print(repeat_n(s, 12))
      # abaabaabaaba
      

      n // len(s) 为您提供了我们可以重复 s 并保持在 n 字符以下的次数,因此 n // len(s) + 1 是我们可以重复 s 的次数以获得至少 n 字符(加上可能还有其他人)。

      使用这个数字,我们只需用s * (n // len(s) + 1) 重复字符串,然后取第一个n 字符(s * (n // len(s) + 1))[:n]

      希望这会有所帮助。

      编辑:为 python 3 修复

      【讨论】:

      • TypeError: 不能将序列乘以“float”类型的非整数
      • 我假设是 python 3,使用 // 代替:return (s * (n // int(len(s)) + 1))[:n]
      【解决方案3】:

      你要找的是模(%)运算符:

      s = 'aba'
      n = 10
      
      out = ''
      for i in range(n):
          out += s[i % len(s)]
      
      print(out)
      

      打印:

      abaabaabaa
      

      【讨论】:

        【解决方案4】:

        这是另一个选项,使用 * 运算符处理字符串:

        s = 'aba'
        n = 10
        
        out = (s * (n // len(s)))[:n]
        print(out)
        

        分解:

        n // len(s) => This is just the number of whole times s will need to be repeated
        + 1        => We add one to this, so our string will be a little longer than needed
        s *        => This repeats s that number of times
        [:n]       => Takes the first n characters of the string
        

        所以虽然它不那么可读,但它非常快。与接受的答案相比:

        def original_calc():
          out = ''
          for i in range(n):
            out += s[i % len(s)]
          result = out
        
        def new_calc():
            result = (s * (n // len(s)))[:n]
        
        import timeit
        s = 'aba'
        n = 10
        >>> timeit.timeit(original_calc, number=100000)
        0.20508930005598813
        >>> timeit.timeit(new_calc, number=100000)
        0.027835099957883358
        

        【讨论】:

        • 另一个答案已经提到过这个解决方案,需要+1 来处理n 不能被len(s) 整除的情况。
        • 您可以尝试与l = len(s); result = s * (n // l) + s[:n % l] 进行比较。
        猜你喜欢
        • 2010-12-01
        • 2020-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-10
        • 2013-05-18
        • 2011-01-09
        相关资源
        最近更新 更多