标签:之间 一个 独立 nbsp 删除元素 索引 ack tuple 插入
>>> 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‘]
aList.append(‘追加‘)
print(aList)
[‘Lori‘, ‘Martins‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘, ‘追加‘]
aList.insert(2,"插入") #不能批量insert
print(aList)
[‘Lori‘, ‘Martins‘, ‘插入‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘, ‘追加‘]
aList[1]="修改" #替换原来的Martins
print(aList)
[‘Lori‘, ‘修改‘, ‘插入‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘, ‘追加‘]
方法1 - list. remove("...")
aList.remove("Lori") #删除方法1
print(aList)
[‘修改‘, ‘插入‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘, ‘追加‘]
方法2 - del
del aList[0] #删除方法2
print(aList)
[‘修改‘, ‘插入‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘, ‘追加‘]
方法3 - list.pop()
aList.pop() #删除方法3,默认最后一位
print(aList)[‘Lori‘, ‘修改‘, ‘插入‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘]
bList = [‘Wayne‘,‘Marcus‘]
aList.extend(bList)
print(aList)
[‘Lori‘, ‘Martins‘, ‘Marcus‘, ‘Fayes‘, ‘Anthony‘, ‘Paul‘, ‘Miranda‘, ‘Ketty‘, ‘Wayne‘, ‘Marcus‘]
print(aList.index("Marcus")) #获取下标
2
print(aList[aList.index("Marcus")]) #对应获取下标的打印元素
Marcus
print(aList.count("Marcus"))
2
aList.sort()
print(aList)
[‘Anthony‘, ‘Fayes‘, ‘Ketty‘, ‘Lori‘, ‘Marcus‘, ‘Marcus‘, ‘Martins‘, ‘Miranda‘, ‘Paul‘, ‘Wayne‘]
aList.reverse() #翻转
print(aList)
[‘Wayne‘, ‘Paul‘, ‘Miranda‘, ‘Martins‘, ‘Marcus‘, ‘Marcus‘, ‘Lori‘, ‘Ketty‘, ‘Fayes‘, ‘Anthony‘]
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‘]
import copy
aList = [‘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)
Lori
Martins
Marcus
Fayes
Anthony
Paul
Miranda
Ketty
元祖是只读列表, 即一旦创建不可修改。 列表用 [], 而元组用()。针对元祖,只可以用count 和 index
aTuple = (‘alex‘,‘jack‘)
Python 基础 - Day 2 Learning Note - 列表&元组
标签:之间 一个 独立 nbsp 删除元素 索引 ack tuple 插入
原文地址:http://www.cnblogs.com/lg100lg100/p/7074346.html