# # 定义一个函数, 实现功能:移除字符串中的指定索引位置(可以指定多个索引)的字符,返回新的字符串# 解法一 索引(当然切片也可以做)defstr_pop(old_str:str,*args:int)->str:ifmin(args)<-len(old_str)ormax(args)>len(old_str)-1:return"Index out of range(您的索引超出范围)"
new_str =''
lst =list(args)for index inrange(len(lst)):if lst[index]<0:
lst[index]=len(old_str)+ lst[index]
index_set =set(lst)for i inrange(len(old_str)):if i in index_set:continue
new_str += old_str[i]return new_str
my_str ="hello numpy"print(str_pop(my_str,1,1,1))print(str_pop(my_str,1,2,3))print(str_pop(my_str,-1,0,1))
lst =[1,2,3]for i in lst:print(i)
i =2print(lst)# 解法二 转列表操作,将对应删除的索引位置变为空,然后字符串拼接返回defstr_pop(string:str,*index)->str:
lst =list(string)for i in index:
lst[i]=""return"".join(lst)
my_string ="hello numpy"print(str_pop(my_string,1))