insert()是Python中的内置函数,可将给定元素插入列表中的给定索引。

用法:

list_name.insert(index, element)

参数:

index - the index at which the element has to be inserted.

element - the element to be inserted in the list.

返回值:

This method does not return any value but

it inserts the given element at the given index.

错误:

If anything other then a list is used with

insert(), then it returns an AttributeError.

注意:如果给定索引> = length(list),则它将插入列表的末尾。

代码1:

# Python3 program for use

# of insert() method

list1 = [ 1, 2, 3, 4, 5, 6, 7 ]

# insert 10 at 4th index

list1.insert(4, 10)

print(list1)

list2 = ['a', 'b', 'c', 'd', 'e']

# insert z at the front of the list

list2.insert(0, 'z')

print(list2)

输出:

[1, 2, 3, 4, 10, 5, 6, 7]

['z', 'a', 'b', 'c', 'd', 'e']

代码2:

# Python3 program for error

# of insert() method

# attribute error

string = "1234567"

string.insert(10, 1)

print(string)

输出:

Traceback (most recent call last):

File "/home/2fe54bd8723cd0ae89a17325da8b2eb5.py", line 7, in

string.insert(10, 1)

AttributeError:'str' object has no attribute 'insert'

实际应用:

在任何元素之前的列表中插入:

代码3:

# Python3 program for Insertion in a list

# before any element using insert() method

list1 = [ 1, 2, 3, 4, 5, 6 ]

# Element to be inserted

element = 13

# Element to be inserted before 3

beforeElement = 3

# Find index

index = list1.index(beforeElement)

# Insert element at beforeElement

list1.insert(index, element)

print(list1)

输出:

[1, 2, 13, 3, 4, 5, 6]

python中insert()函数的用法_Python list insert()用法及代码示例相关推荐

  1. python中mode函数怎么用_Python Wand mode()用法及代码示例

    原始图片: 范例1: # Import library from Image from wand.image import Image # Import the image with Image(fi ...

  2. python open 打开是什么类型的文件-详解Python中open()函数指定文件打开方式的用法...

    文件打开方式 当我们用open()函数去打开文件的时候,有好几种打开的模式. 'r'->只读 'w'->只写,文件已存在则清空,不存在则创建. 'a'->追加,写到文件末尾 'b'- ...

  3. python中append函数什么意思_python中append函数用法讲解

    python中append函数用法讲解 如果在做一个地区的统计工作,可以使用列表来帮助我们.输入汉字或者其他字符,比如"01代表汉族",那么在写民族的时候有下拉列表,就可以打01, ...

  4. python中reduce函数的运用_python 中 reduce 函数的使用

    reduce()函数也是Python内置的一个高阶函数. reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接 ...

  5. python中定义函数常用关键字_Python 中定义函数的关键字是 _________________ 。_学小易找答案...

    [其它]实验4-串和数组-实验任务书.docx [填空题]表达式 'abc' in ['abcdefg'] 的值为______________. [填空题]已知 x = range(1,4) 和 y ...

  6. python中write函数%啥意思_python write( )函数

    (交流学习群 467604262) 在博客(python 本地数据获取   网址:http://blog.csdn.net/sxingming/article/details/51333663)中,我 ...

  7. python中int函数的功能_python中int函数是什么意思

    摘要 腾兴网为您分享:python中int函数是什么意思,小t智联,未来屋,唯品会,围棋宝典等软件知识,以及么么聊天,daemontoolslite,鼠标自动连点器,河南校信通,时尚魔女,帮兼职,红包 ...

  8. python中choice()函数的参数_Python中choice函数如何实现?

    熟悉Python的小伙伴是知道Python是可以生成随机项的,python中choice函数是random模块的随即取样函数,它可以通过导入 random 模块,调用 random 静态对象生成Pyt ...

  9. python中使用函数的优点_Python基础之函数基本用法与进阶详解

    本文实例讲述了Python基础之函数基本用法与进阶.分享给大家供大家参考,具体如下: 目标 函数参数和返回值的作用 函数的返回值 进阶 函数的参数 进阶 递归函数 01. 函数参数和返回值的作用 函数 ...

  10. python中sorted函数逆序_Python中sorted函数的用法(转)

    [Python] sorted函数 我们需要对List.Dict进行排序,Python提供了两个方法 对给定的List L进行排序, 方法1.用List的成员函数sort进行排序,在本地进行排序,不返 ...

最新文章

  1. 使用jdk生成ssl证书文件
  2. 如何提高增加包含大量记录的表的主键字段的效率
  3. 中国移动与苹果联姻 三星在华霸主地位或遭取代
  4. 免费公开课 | 数据科学家,从入门到精进!【今晚福利】
  5. Python做web前后端交互
  6. php开发mvc教程,php开发一个简单的MVC
  7. [新闻]Ubuntu7.04于4月19日全球同步发布
  8. python中assert_在Python中带有示例的assert关键字
  9. Laravel中的env函数获取不到确定存在的配置
  10. 51CTO六周年,我不得不说的话!【我与51CTO的故事】
  11. Android API 中文 (54) —— Filterable
  12. python123反素数_初学python之路-day01
  13. 常用通信光纤是如何分类的
  14. 3A算法—自动曝光(AE)
  15. 建筑设计的未来是什么?| 建筑 · 人工智能专栏
  16. ES DSL搜索 - prefix、fuzzy和wildcard
  17. VMware VCP 认证考试电子版证书下载方式
  18. oracle根据约束名查找具体信息,如是哪张表的哪个字段约束信息
  19. 【GStreamer】gstreamer sink中对比度contrast、亮度brightness、饱和度saturation、hue 概念理解
  20. ata职业技能评价证书考出来有啥用?

热门文章

  1. 关于 MSDTC 分布式事务两个常见错误
  2. JDBC与ORM发展与联系 JDBC简介(九)
  3. Powershell 自定义输出列,两个例子
  4. BarTender安装常见问题集结
  5. .NET中判断国内IP和国外IP
  6. 100M不限流量香港VPS服务器租用注意事项
  7. 传统云主机存在哪些问题和弊端?
  8. 【指标统计】统计装置总招遥信
  9. Jmeter工具笔记-Jmeter+influxdb+grafanas设置性能监控过程
  10. 20165301第十周课下补做