Slice view
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,”ChenRonghua”,”XuLiangChen”]
print(names)
print(names[1:3]) ##Slicing; regardless of the head and tail
print(names[0:3])
print(names[-1])
print(names[-2:])
print(names[:3])
print(names[0],names[2])
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’]
[‘4Guyun’, ‘Xiangpeng’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’]
XuLiangChen
[‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’]
#ZhangYang Xiedi
Append
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,”ChenRonghua”,”XuLiangChen”]
print(names)
names.append(‘LeiHaidong’)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
Insert
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,”ChenRonghua”,”XuLiangChen”]
print(names)
names.append(‘LeiHaidong’)
names.insert(1,”ChenRonghua”)
names.insert(3,”XingZhiyu”)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘ChenRonghua’, ‘4Guyun’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
Replace
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,”ChenRonghua”,”XuLiangChen”]
print(names)
names.append(‘LeiHaidong’)
names.insert(1,”ChenRonghua”)
names.insert(3,”XingZhiyu”)
names[2] =’Xiedi’
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘ChenRonghua’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
Delete
Method 1:
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,”ChenRonghua”,”XuLiangChen”]
print(names)
names.append(‘LeiHaidong’)
names.insert(1,”ChenRonghua”)
names.insert(3,”XingZhiyu”)
names[2] =’Xiedi’
print(names)
#delete(three methods)
names. remove(‘ChenRonghua’)
#del names[1]=names. pop(1)
#names.pop(1)
print(names)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘ChenRonghua’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
[‘#ZhangYang’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
Method 2:
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,”ChenRonghua”,”XuLiangChen”]
print(names)
names.append(‘LeiHaidong’)
names.insert(1,”ChenRonghua”)
names.insert(3,”XingZhiyu”)
names[2] =’Xiedi’
print(names)
#delete(three methods)
#names. remove(‘ChenRonghua’)
#del names[1]=names. pop(1)
del names[1]
#names.pop(1)
print(names)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘ChenRonghua’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
[‘#ZhangYang’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
Method Three:
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,”ChenRonghua”,”XuLiangChen”]
print(names)
names.append(‘LeiHaidong’)
names.insert(1,”ChenRonghua”)
names.insert(3,”XingZhiyu”)
names[2] =’Xiedi’
print(names)
#delete(three methods)
#names. remove(‘ChenRonghua’)
#del names[1]=names. pop(1)
#del names[1]
names. pop(1)
print(names)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘ChenRonghua’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
[‘#ZhangYang’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
Find
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,”ChenRonghua”,”XuLiangChen”]
print(names)
names.append(‘LeiHaidong’)
names.insert(1,”ChenRonghua”)
names.insert(3,”XingZhiyu”)
names[2] =’Xiedi’
print(names)
print(names. index(“Xiedi”))
print(names[names. index(“Xiedi”)])
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘ChenRonghua’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
2
Xiedi
Number
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,”ChenRonghua”,”XuLiangChen”]
print(names)
names.append(‘LeiHaidong’)
names.insert(1,”ChenRonghua”)
names.insert(3,”XingZhiyu”)
names[2] =’Xiedi’
print(names)
print(‘ChenRonghua:’,names.count(“ChenRonghua”))
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘ChenRonghua’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
Chen Ronghua: 2
Clear
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,”ChenRonghua”,”XuLiangChen”]
print(names)
names.append(‘LeiHaidong’)
names.insert(1,”ChenRonghua”)
names.insert(3,”XingZhiyu”)
names[2] =’Xiedi’
print(names)
names. clear()
print(names)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘ChenRonghua’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
[]
Order flipping and ASCII sorting
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,”ChenRonghua”,”XuLiangChen”]
print(names)
names.append(‘LeiHaidong’)
names.insert(1,”ChenRonghua”)
names.insert(3,”XingZhiyu”)
names[2] =’Xiedi’
print(names)
names.reverse() ##order flip
print(names)
names.sort() #Flip in ASCII order
print(names)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘ChenRonghua’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
[‘LeiHaidong’, ‘XuLiangChen’, ‘ChenRonghua’, ‘Xiangpeng’, ‘XingZhiyu’, ‘Xiedi’, ‘ChenRonghua’, ‘#ZhangYang’]
[‘#ZhangYang’, ‘ChenRonghua’, ‘ChenRonghua’, ‘LeiHaidong’, ‘Xiangpeng’, ‘Xiedi’, ‘XingZhiyu’, ‘XuLiangChen’]
Append list to list
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,”ChenRonghua”,”XuLiangChen”]
print(names)
names.append(‘LeiHaidong’)
names.insert(1,”ChenRonghua”)
names.insert(3,”XingZhiyu”)
names[2] =’Xiedi’
print(names)
names2 = [1,2,3,4]
names. extend(names2)
print(names,names2)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘ChenRonghua’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
[‘#ZhangYang’, ‘ChenRonghua’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’, 1, 2, 3, 4] [1, 2, 3, 4]
Delete list
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,”ChenRonghua”,”XuLiangChen”]
print(names)
names.append(‘LeiHaidong’)
names.insert(1,”ChenRonghua”)
names.insert(3,”XingZhiyu”)
names[2] =’Xiedi’
print(names)
names2 = [1,2,3,4]
names. extend(names2)
del names2
print(names,names2)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’]
Traceback (most recent call last):
[‘#ZhangYang’, ‘ChenRonghua’, ‘Xiedi’, ‘XingZhiyu’, ‘Xiangpeng’, ‘ChenRonghua’, ‘XuLiangChen’, ‘LeiHaidong’]
File “D:/PycharmProjects/zhuyouen/0403/names.py”, line 35, in
print(names,names2)
NameError: name ‘names2’ is not defined ——->Because there is no list ‘name2’, there is no definition
Slice view
Method 1:
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,[‘alex’,’jack’],”ChenRonghua”,”XuLiangChen”]
for i in range(1,6,2):
print(names[i])
print()
for i in names:
print(i)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
4Guyun
[‘alex’, ‘jack’]
XuLiangChen
#ZhangYang
4Guyun
Xiang Peng
[‘alex’, ‘jack’]
Chen Ronghua
XuLiangChen
Method 2:
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,[‘alex’,’jack’],”ChenRonghua”,”XuLiangChen”]
print(names)
print(names[0:-1:2])
print(names[::2])
print(names[1::2]
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘Xiangpeng’, ‘ChenRonghua’]
[‘#ZhangYang’, ‘Xiangpeng’, ‘ChenRonghua’]
[‘4Guyun’, [‘alex’, ‘jack’], ‘XuLiangChen’]
Copy
Method 1:
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,[‘alex’,’jack’],”ChenRonghua”,”XuLiangChen”]
name2 = names. copy()
print(names)
print(name2)
print()
names[2] = “Xiang Peng”
names[3][0] = “ALEX”
print(names)
print(name2)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiang Peng’, [‘ALEX’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘ALEX’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
Method 2:
# author = “zhuyouen”
import copy
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,[‘alex’,’jack’],”ChenRonghua”,”XuLiangChen”]
name2 = copy. copy(names)
print(names)
print(name2)
print()
names[2] = “Xiang Peng”
names[3][0] = “ALEX”
print(names)
print(name2)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiang Peng’, [‘ALEX’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘ALEX’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
Method Three:
# author = “zhuyouen”
import copy
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,[‘alex’,’jack’],”ChenRonghua”,”XuLiangChen”]
name2 = copy.deepcopy(names)
print(names)
print(name2)
print()
names[2] = “Xiang Peng”
names[3][0] = “ALEX”
print(names)
print(name2)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiang Peng’, [‘ALEX’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
Copy
# author = “zhuyouen”
##shallow copy
person=[‘name’,[‘saving’,100]]
”’
p1=copy.copy(person)
p2=person[:]
p3=list(person)
”’
p1=person[:]
p2=person[:]
p1[0]=’alex’
p2[0]=’fengjie’
p1[1][1]=50
print(p1)
print(p2)
names[3][0] = “ALEX”
print(names)
print(name2)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiang Peng’, [‘ALEX’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘ALEX’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
Method 2:
# author = “zhuyouen”
import copy
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,[‘alex’,’jack’],”ChenRonghua”,”XuLiangChen”]
name2 = copy. copy(names)
print(names)
print(name2)
print()
names[2] = “Xiang Peng”
names[3][0] = “ALEX”
print(names)
print(name2)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiang Peng’, [‘ALEX’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘ALEX’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
Method Three:
# author = “zhuyouen”
import copy
names = [“#ZhangYang”,”4Guyun”,”Xiangpeng”,[‘alex’,’jack’],”ChenRonghua”,”XuLiangChen”]
name2 = copy.deepcopy(names)
print(names)
print(name2)
print()
names[2] = “Xiang Peng”
names[3][0] = “ALEX”
print(names)
print(name2)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0403/names.py
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiang Peng’, [‘ALEX’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
[‘#ZhangYang’, ‘4Guyun’, ‘Xiangpeng’, [‘alex’, ‘jack’], ‘ChenRonghua’, ‘XuLiangChen’]
Copy
# author = “zhuyouen”
##shallow copy
person=[‘name’,[‘saving’,100]]
”’
p1=copy.copy(person)
p2=person[:]
p3=list(person)
”’
p1=person[:]
p2=person[:]
p1[0]=’alex’
p2[0]=’fengjie’
p1[1][1]=50
print(p1)
print(p2)