列表
tt='hello'
list1=[1,4,tt,3.4,'yes',[1,2]]
print(list1,id(list1))
#[1, 4, 'hello', 3.4, 'yes', [1, 2]] 1867157683400
比较list中添加元素的几种方法的区别
list3=[6,7]
l2=list1+list3
print(l2,id(id))
#[1, 4, 'hello', 3.4, 'yes', [1, 2]] 1867161085256
[1, 4, 'hello', 3.4, 'yes', [1, 2], 6, 7] 1867039675448
重新生成列表
l2=list1.extend(list3)
print(l2,id(l2))
print(list1,id(list1))
#None 140709734730976
#[1, 4, 'hello', 3.4, 'yes', [1, 2], 6, 7] 1867161111368
tt='hello'
list1=[1,4,tt,3.4,'yes',[1,2]]
#rint(list1,id(list1))
#[1, 4, 'hello', 3.4, 'yes', [1, 2]] 1867157683400
list3=[6,7]
l2=list1.append(list3)
print(l2,id(l2))
print(list1,id(list1))
#None 140709734730976
#[1, 4, 'hello', 3.4, 'yes', [1, 2], [6, 7]] 1867161086600
删除操作演示
tt='hello'
list1=[1,4,tt,3.4,'yes',[1,2]]
del list1[2:5]
print(list1)
del list1[2]
print(list1)
#[1, 4, [1, 2]]
[1, 4]
删除变量操作
```python
tt='hello'
list1=[1,4,tt,3.4,'yes',[1,2]]
l2=list1
print(id(l2),id(list1))
del list1
print(l2)
print(list1)#删除变量,只是销毁变量,并没有删除指向的数据
#1867157255944 1867157255944
[1, 4, 'hello', 3.4, 'yes', [1, 2]]
删除数据
tt='hello'
list1=[1,4,tt,3.4,'yes',[1,2]]
l2=list1
"""print(id(l2),id(list1))
del list1
print(l2)
print(list1)"""
l3=l2
del l2[:]
print(l2)
print(l3)
#[]
#[]
list实现队列和栈
queue=[ ]
queue.insert(0,1)
queue.insert(0,2)
queue.insert(0,"hello")
print("取第一个元素",queue.pop())
print("取第二个元素",queue.pop())
print("取第三个元素",queue.pop())
#取第一个元素 1
#取第二个元素 2
#取第三个元素 hello
satck=[ ]
satck.append(1)
satck.append(2)
satck.append("hello")
print("取第一个元素",satck.pop())
print("取第二个元素",satck.pop())
print("取第三个元素",satck.pop())
#取第一个元素 hello
取第二个元素 2
取第三个元素 1
元组
t=(3)
t1=(3,)
li=[3]
print(t,t1,li)
#3 (3,) [3]
tt='hello'
t1=(1,4,tt,3.4,'yes',[1,2])
print(t1)
t1[5][0]="3445"
print(t1)
#(1, 4, 'hello', 3.4, 'yes', [1, 2])
(1, 4, 'hello', 3.4, 'yes', ['3445', 2])
版权声明:本文为weixin_43250159原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。