python重复list中元素中的字母次数

来源:互联网 发布:ife矩阵结果分析 编辑:程序博客网 时间:2024/06/06 20:24
例如实现以下的情况:
words = ['hello', 'exercise', 'with', 'words']
repeat_letter(words, [4, 2, 10, -2], 3)搜索

#得到 ['helloooo', 'exeeeercise', 'with', 'wordddds']



def repeat_letter(words, positions, times):
    # 这里用了个Python切片特性的小技巧,w[p]如果p超出索引范围会报错。
    # 但是如果写成w[p:p+1]就不会报错,而是返回一个w的空值,这样就免去了判断 if p < len(w) and p >= -len(w)
    return list(map(lambda w,p: w[:p] + w[p:p+1]*(times+1+ w[p+1:], words, positions))
 
words = ['hello''exercise''with''words']
 
print(repeat_letter(words, [4210-2], 3))


原创粉丝点击