python - 如何通过使用python将现有值移动到下一个索引来在特定索引中的现有列表中插入值
问题描述
我有如下列表。
test = ['firstvalue', 'thirdvalue']
我想将一些值插入到列表中。索引 1 处的第二个值和索引 3 处的第四个值
所以输出列表如下所示
test = ['firstvalue', 'secondvalue', 'thirdvalue', 'fourthvalue']
我尝试了以下方式,但它对我不起作用
print test.insert(1, "secondvalue")
有没有其他方法可以做到这一点?
解决方案
insert 函数不返回值,而是修改其上使用的数组。这是一个例子:
test = ['firstvalue', 'thirdvalue']
test.insert(1, "secondvalue")
print test
推荐阅读
- mysql - 为什么“Multi-statement transaction required more than 'max_binlog_cache_size' byte s of storage”异常只发生在部分从站中
- android - gemfile 将如何维护,作为 iOS 和 Android 在 react-native 应用程序中的通用文件
- python - AWS 精确定位 | 有没有办法向拥有该应用程序的所有用户发送精确通知?
- excel - 我正在寻找一种方法来从工作表 C 中选择一个值,将其与工作表 A 中的一个值匹配,然后从 A 填充 C
- javascript - 将新数组动态添加到现有对象数组 React JS
- r - R - 小值的错误计算
- php - 如何根据从codeigniter下拉列表中选择的类别定向到表单页面?
- react-native - 在 react-native 中将数据从一个屏幕传递到另一个屏幕时获取未定义的值
- javascript - 用柏树在 jquery 日历中选择日期
- git - git忽略更改但不删除文件