第1个回答 2017-10-19
words = ['hello', 'exercise', 'with', 'words']
def repeat_letter(words,index,num):
return_worlds=[]
for i in range(0,len(words)):
if len(words[i]) > index[i]:
word_list=list(words[i])
range_word=word_list[index[i]]*(num+1)
word_list[index[i]]=range_word
new_word=''.join(word_list)
else:
new_word =words[i]
return_worlds.append(new_word)
return return_worlds
print repeat_letter(words, [4, 2, 10, -2], 3)
# words = ['helloooo', 'exeeeercise', 'with', 'wordddds']本回答被提问者采纳
第2个回答 2017-10-19
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
def repeat_letter(word_list,handle_list,repeat_cycles):
for i in range(min(len(word_list),len(handle_list))):
words = list(word_list[i])
if handle_list[i] < len(words):
word = words[handle_list[i]]
word = word*(repeat_cycles+1)
words[handle_list[i]] = word
word_list[i] = "".join(words)
print (word_list)
return word_list本回答被网友采纳
第3个回答 2017-10-19
def repeat_letter(words, positions, times):
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, [4, 2, 10, -2], 3))
# ['helloooo', 'exeeeercise', 'with', 'wordddds']