1. 列表 LIST
-
定义列表
>>> alist=[‘Lydia‘,‘Miranda‘,‘Tony‘,‘Cody‘,‘Lorries‘]
>>> print(aList)
[‘Tom‘, ‘Ruby‘, ‘Eddie‘, ‘Eric‘]
-
通过下标访问列表中的元素
>>> alist[0]
‘Lydia‘
>>> alist[3]
‘Cody‘
>>> alist[-1]
‘Lorries‘
>>> alist[-4]
‘Miranda‘
- 切片操作:取多个元素。 序列类型是其元素被顺序放置的一种数据结构类型,这种方式允许通过指定下标的方式来获得某一个数据元素,或者通过指定下标范围来获得一组序列的元素。( [],[:],[::])
aList = [‘Lori‘, ‘Martins‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘]print(aList[1:4]) # 自左向右,从0计数。取下标1至下标4之间的数字,包括1,不包括[‘Martins‘, ‘Marcus‘, ‘Fayes‘] print(aList[1:-1]) # 取下标1到下标-1的值,不包括-1. -1是ketty[‘Martins‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘]
print(aList[0:3])[‘Lori‘, ‘Martins‘, ‘Marcus‘]print(aList[:3]) # 0可以忽略,和上句效果一样[‘Lori‘, ‘Martins‘, ‘Marcus‘]
print(aList[3:]) #如果想取到最后一个值,不能写-1,只能这么写[‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘]print(aList[3:-1]) #这样下标-1的元素不会被包括[‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘] 切片操作的扩展 - 步长索引print(aList[::2]) # 第三个索引值2被用作步长参数,代表每隔1个元素就取一下[‘Lori‘, ‘Marcus‘, ‘Anthony‘, ‘Miranda‘]
print(aList[::-1]) # 效果等同“reverse”操作[‘Ketty‘, ‘Miranda‘, ‘Paul‘, ‘Anthony‘, ‘Fayes‘, ‘Marcus‘, ‘Martins‘, ‘Lori‘]
-
追加 list.append
aList.append(‘追加‘)print(aList)[‘Lori‘, ‘Martins‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘, ‘追加‘]
-
插入元素 list.insert
aList.insert(2,"插入") #不能批量insertprint(aList)[‘Lori‘, ‘Martins‘, ‘插入‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘, ‘追加‘]
-
替换修改元素
aList[1]="修改" #替换原来的Martinsprint(aList)[‘Lori‘, ‘修改‘, ‘插入‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘, ‘追加‘]
-
删除元素
方法1 - list. remove("...")
aList.remove("Lori") #删除方法1print(aList)[‘修改‘, ‘插入‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘, ‘追加‘] 方法2 - del
del aList[0] #删除方法2print(aList)[‘修改‘, ‘插入‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘, ‘追加‘]
方法3 - list.pop()
aList.pop() #删除方法3,默认最后一位print(aList)[‘Lori‘, ‘修改‘, ‘插入‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘]
-
扩展(合并)list.extend(list1)
bList = [‘Wayne‘,‘Marcus‘]aList.extend(bList)print(aList)[‘Lori‘, ‘Martins‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘, ‘Wayne‘, ‘Marcus‘]
-
获取下标 list.index
print(aList.index("Marcus")) #获取下标2print(aList[aList.index("Marcus")]) #对应获取下标的打印元素Marcus
-
统计 list.count
print(aList.count("Marcus"))2
-
排序 list.sort
aList.sort()print(aList)[‘Anthony‘, ‘Fayes‘, ‘Ketty‘, ‘Lori‘, ‘Marcus‘, ‘Marcus‘, ‘Martins‘, ‘Miranda‘, ‘Paul‘, ‘Wayne‘]
-
反转 list.reverse
aList.reverse() #翻转print(aList)[‘Wayne‘, ‘Paul‘, ‘Miranda‘, ‘Martins‘, ‘Marcus‘, ‘Marcus‘, ‘Lori‘, ‘Ketty‘, ‘Fayes‘, ‘Anthony‘]
-
拷贝 - 浅copy
aList1 = aList.copy()aList[2]=‘MARCUS WOO‘aList[4][0]=‘alexander‘ print(aList)[‘Lori‘, ‘Martins‘, ‘MARCUS WOO‘, ‘Fayes‘, [‘alexander‘, ‘pete‘], ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘]print(aList1) #浅copy, 只copy第一层。这是由于list的存储方式导致的,alexander会改的原因是因为具有相同的内存地址。而第一层不是。 [‘Lori‘, ‘Martins‘, ‘Marcus‘, ‘Fayes‘, [‘alexander‘, ‘pete‘], ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘]
-
拷贝 - deepcopy 互相完全独立
import copyaList = [‘Lori‘, ‘Martins‘, ‘Marcus‘, ‘Fayes‘, [‘alex‘,‘pete‘],‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘] aList2 = copy.deepcopy(aList) #最终两个list完全独立aList[2]=‘MARCUS WOO‘aList[4][0]=‘alexander‘print(aList)[‘Lori‘, ‘Martins‘, ‘MARCUS WOO‘, ‘Fayes‘, [‘alexander‘, ‘pete‘], ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘]print(aList2)[‘Lori‘, ‘Martins‘, ‘Marcus‘, ‘Fayes‘, [‘alex‘, ‘pete‘], ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘]
-
循环
for i in aList: print(i)LoriMartinsMarcusFayesAnthonyPaulMirandaKetty
2. 元组 TUPLE
元祖是只读列表, 即一旦创建不可修改。 列表用 [], 而元组用()。针对元祖,只可以用count 和 index
aTuple = (‘alex‘,‘jack‘)
时间: 2024-12-17 23:39:41