代码之家  ›  专栏  ›  技术社区  ›  agenis

创建重复字符数增加的列表[关闭]

  •  1
  • agenis  · 技术社区  · 6 年前

    ["k", "kk", "kkk", "kkkk", ...]
    ["rep", "reprep", "repreprep", ...]
    

    4 回复  |  直到 6 年前
        1
  •  1
  •   Taohidul Islam    6 年前
    repeating_value = "k" #Assign the value which do you want to be repeated
    total_times=5 #how many times do you want
    expected_list=[repeating_value*i for i in range(1,total_times+1)]
    print(expected_list)
    
        2
  •  2
  •   Netwave    6 年前

    这里有一台发电机 itertools.count ,请记住python中“乘”字符串的属性,它们将被复制并连接到该属性中。 nth 时间,例如在哪里 "a"*3 == "aaa" 以下内容:

    import itertools
    def genSeq(item):
        yield from (item*i for i in itertools.count())
    

    给你一个 live example

        3
  •  0
  •   Simeon Ikudabo    6 年前
    character = 'k'
    _range = 5
    
    output = [k*x for k in character for x in range(1, _range + 1)]
    print(output)
    

    我将在这个范围内用指定的数字将我的字符乘以倍数,然后在列表理解中简单地迭代这个范围。我们在范围的末尾加1以得到完整的范围。

    ['k', 'kk', 'kkk', 'kkkk', 'kkkkk']
    
        4
  •  0
  •   Taohidul Islam    6 年前

    var = 'rep'
    list = [var * i for i in range(1,x,1)]
    print(list)