Python len() list pop() append() insert() remove() copy()

本文详细介绍了Python中列表的各种操作方法,包括元素的增删改查、切片、扩展及浅拷贝等,并通过实例展示了常见函数如insert、remove、pop、extend等的使用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

world = "0123456789"
worldList = list(world)
# 打印数组,猜测打印一个变量X,就相当于打印str(X),而str(X)猜测相当于对X发送一个消息,X返回对自己的描述
print(worldList)
print(str(worldList))

length = len(world)
print("数组的长度: " + str(length))

ch0 = worldList[0]
print(ch0)

worldList.remove(ch0)  #参数不是索引值而是要删除的值

#默认弹出最后一个数据
chLast = worldList.pop()
print("pop()默认弹出最后一个数据: " + chLast)
    
print(worldList)

ch1 = worldList.pop(0)
print("pop(0) 也可以弹出指定位置的数据: " + ch1)
print(worldList)

worldList.insert(0,ch0) # 第一个参数为Index,第二个参数为要插入的值
worldList.insert(1,ch1)
worldList.insert(len(worldList),chLast)
print("list.insert(index,x):把x插入到list中,新元素的索引就是index:" + str(worldList))

# Python中变量作用域
if len(worldList) > 0:
    chTest = worldList.pop(0)

print("chTest 赋值语句与此行代码并不在同一个代码组,按照C语言中的理念打印此变量会报错:"
      + str(chTest)) #实际上没有报错


# 尝试在最后一个索引后面第二个位置插入数据
print(worldList)  # ['1', '2', '3', '4', '5', '6', '7', '8', '9']
worldList.insert(len(worldList)+10,"测试")
print(worldList)  # 完全没有问题:['1', '2', '3', '4', '5', '6', '7', '8', '9', '测试']


numList = [0,1,2,3,4,5]
numList.extend([6,7,8])
numList.insert(2,1.1)
print("测试extend()函数:" + str(numList)) # 测试extend()函数:[0, 1, 1.1, 2, 3, 4, 5, 6, 7, 8]

chList = list('abcdefghijklmn') # ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
print(chList)
newChList = 'H'.join(chList)
print("str的join()方法: " + str(newChList)) # str的join()方法: aHbHcHdHeHfHgHhHiHjHkHlHmHn

#上面的join()方法能够正常运行,这个则不行
#newNumList = 'H'.join(numList) # TypeError: sequence item 0: expected str instance, int found
#print(newNumList)


list0 = [1,2,3,4,5,6,7,8]
list1 = list0
list0.extend([9,10,11])

list2 = list1.copy()
list1.extend([12,13,14,15])
print("浅拷贝:" + str(list1))
print("深拷贝: " + str(list2)) 

# start 默认值0,stop默认值是取列表允许的最大值,step默认值为1
print("list1 :" + str(list1))  # list1 :[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
print("list1[1] :" + str(list1[1]))  # list1[1] :2
print("list1[2] :" + str(list1[2]))  # list1[1] :3
print("list1[3] :" + str(list1[3]))  # list1[1] :4
print("list1[-1] :" + str(list1[-1])) # list1[1] :15
print("list1[-2] :" + str(list1[-2]))  # list1[1] :14
print("list1[-2] :" + str(list1[-3]))  # list1[1] :13
print("list1[0::2] :" + str(list1[0::2]))  # list1[0::2] :[1, 3, 5, 7, 9, 11, 13, 15]
print("list1[3:] :" + str(list1[3:]))  # list1[3:] :[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
print("list1[:10] :" + str(list1[:10])) # list1[:10] :[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # 索引从0开始到10,包括0但不包括9
print("list1[-3::] :" + str(list1[-3::]))  # list1[-3::] :[13, 14, 15]
print("list1[0:100:1] :" + str(list1[0:100:1]))  # list1[0:100:1] :[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
print("list1[0:3:1] :" + str(list1[0:3:1]))  # list1[0:3:1] :[1, 2, 3]


# Python中list.remove(x)方法每次都会移除发现的第一个x。
chList = list("abbcccdddd")
print(chList)  # ['a', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'd']
chList.remove('b')  # ['a', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'd']
print(chList)  # ['a', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'd']
chList.remove('c') # 只会除去第一个相同的元素
chList.remove('c')
print(chList)  # ['a', 'b', 'c', 'd', 'd', 'd', 'd']
chList.remove('d')
chList.remove('d')
chList.remove('d')
print(chList)  # ['a', 'b', 'c', 'd']
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值