21番茄网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 21番茄网
查看: 725|回复: 0

python 列表中插入其他元素的方法(append,extend和insert)

[复制链接]

650

主题

805

帖子

2706

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2706
发表于 2021-2-18 14:44:37 | 显示全部楼层 |阅读模式
python 列表中插入其他元素的方法

可以用append,extend和insert
其中,append、extend是在最后插入,extend可以把列表以元素的形式添加到指定列表中。
insert插入位置随意。
list1=['a','b','c','c']
list1.append('time')
print(list1)

list1=['a','b','c','c']
list1.append(['time'])
print(list1)

list1=['a','b','c','c']
list1.extend(['time','test'])
print(list1)

list1=['a','b','c','c']
list1.insert(0,'time')
print(list1)

结果分别为:
['a', 'b', 'c', 'c', 'time']
['a', 'b', 'c', 'c', ['time']]
['a', 'b', 'c', 'c', 'time', 'test']
['time', 'a', 'b', 'c', 'c']
python 列表中插入其他元素的方法(append,extend和insert)  Python基础语法 1
                               
登录/注册后可看大图


参考:https://jingyan.baidu.com/article/da1091fb7ce94c027849d60b.html






上一篇:python的变量名自动增加(递增)的方法
下一篇:python 求多个列表的交集、并集、差集
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|21番茄网 ( 粤ICP备10099428号 ) 奇远富

GMT+8, 2024-5-15 20:00 , Processed in 0.514730 second(s), 46 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表