python - CodingBat 帮助。list.remove(x): x not in list 错误?不明白为什么这不起作用
问题描述
“给定一个非空字符串和一个 int n,返回一个新字符串,其中索引 n 处的 char 已被删除。n 的值将是原始字符串中 char 的有效索引(即 n 将在范围内0..len(str)-1 包括在内)。”
missing_char('kitten', 1) → 'ktten'
missing_char('kitten', 0) → 'itten'
missing_char('kitten', 4) → 'kittn'
我的解决方案:
def missing_char(str, n):
lst = list(str)
lst.remove([n])
return lst
*Compile problems:
list.remove(x): x not in list*
解决方案
你可以试试:
def missing_char(word, index):
if len(word) > index:
word = word[0 : index : ] + word[index + 1 : :]
return word
else:
return "invalid index"
print(missing_char('kitten', 1))
# ktten
推荐阅读
- excel - 如何从另一个单元格引用单元格引用(这会导致另一个工作表单元格引用)
- vb.net - 创建指向文件服务器的链接并将其放入文本框中
- android - 在NavController导航图android中处理导航堆栈?
- node.js - 在不创建超大缓存缓冲区的情况下提高插入效率的更好方法是什么?
- ruby - 在 ruby 中查找嵌套文件路径
- c# - DataGridView 最小小数位数
- php - Facade\Ignition\Exceptions\ViewException 试图获取非对象的属性“名称”
- javascript - 如何显示一定范围的数字为真?
- scip - 如何在 SCIP 中打印变量名?
- java - 如何在单个键下显示所有购物车项目?