当前位置 : 主页 > 编程语言 > python >

#yyds干货盘点#List函数insert - python基础学习系列(67)

来源:互联网 收集:自由互联 发布时间:2022-06-15
以Python 3.x版本为主 insert:值添加到List列表中 1、函数 编号 函数名 说明 1 insert 将值插入到List列表的指定位置 温馨提示:如果下标超出长度,那么也只会默认添加到最后位置 ​代码如

以Python 3.x版本为主

insert:值添加到List列表中

1、函数

编号

函数名

说明

1

insert

将值插入到List列表的指定位置

温馨提示:如果下标超出长度,那么也只会默认添加到最后位置


  • ​代码如下​
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Apr 14, 2022 22:50 AM

import sys

list=['51','CTO']
# 1、值在添加到List中 - insert
target_str='51'
list.insert(5,target_str)
print('')
print('新列表值:%s' % (list))


  • ​效果如下​

#yyds干货盘点#List函数insert - python基础学习系列(67)_python


2、易出错情况
  • 如果只输入一个参数,那么就会报错

#yyds干货盘点#List函数insert - python基础学习系列(67)_指定位置_02

  • 第一个参数为下标,第二参数才为插入的值,顺序不能搞错,否则报错

#yyds干货盘点#List函数insert - python基础学习系列(67)_python_03


上一篇:Tensoflow 把自己的图片生成向量
下一篇:没有了
网友评论