列表:
列表的简单操作:
users = ["yu","jie","fu","Rich","tian"] #print(users) #print(users[0],users[3]) #print(users[1:3]) # 切片 #print(users[-2]) # 输出Rich #print(users[-5:-3]) # 输出 yu,jie #print(users[:3]) '''''''''''增''''''''''' users.append('tain5') #['yu', 'jie', 'fu', 'Rich', 'tian', 'tain5'] 末尾添加 users.insert(1,'yang') # ['yu', 'yang', 'jie', 'fu', 'Rich', 'tian', 'tain5'] 指定位置添加 """""""""改""""""""""""""" users[2] = 'jie1' #['yu', 'yang', 'jie1', 'fu', 'Rich', 'tian', 'tain5'] 修改 """删除""" users.remove("jie1") # 删除指定元素 ['yu', 'yang', 'fu', 'Rich', 'tian', 'tain5'] del users[0] #删除指定位置 ['yang', 'fu', 'Rich', 'tian', 'tain5'] users.pop() # 默认删除最后一个,输入下表删除指定元素 , 返回删除元素 ['yang', 'fu', 'Rich', 'tian'] print(users)
对下标的获取操作
>>> users = ["yu","jie","fu","Rich","tian"]
>>> print(users)
['yu', 'jie', 'fu', 'Rich', 'tian']
>>>
KeyboardInterrupt
>>> print(users.index('fu'))
2
>>> print(users[users.index('fu')])
fu
>>>
>>> users.insert(1,'jie')
>>> users.count('jie') # 统计元素出现次数
2
>>>
>>> print(users)
['yu', 'jie', 'jie', 'fu', 'Rich', 'tian']
>>> users.reverse()
>>> users
['tian', 'Rich', 'fu', 'jie', 'jie', 'yu'] # 翻转列表
>>>
>>> users.sort()
>>> users
['Rich', 'fu', 'jie', 'jie', 'tian', 'yu'] # 排序
>>>
合并
>>> users2 = [1,2,3,4,5]
>>> users.extend(users2)
>>> users
['Rich', 'fu', 'jie', 'jie', 'tian', 'yu', 1, 2, 3, 4, 5]
删除
>>> del users2
>>> users2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'users2' is not defined
>>>
关于浅copy初步认识
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 users = ["yu","jie","fu","Rich","tian",["金","银","铜","铁"]] 2 3 users2 = users.copy() 4 5 print(users) 6 print(users2) 7 print("succ".center(50,'-')) 8 9 users[2] = 'liu' 10 users[5][1] = "铂" 11 12 print(users) 13 print(users2) 14 print("succ".center(50,'-')) 15 16 '''结果['yu', 'jie', 'fu', 'Rich', 'tian', ['金', '银', '铜', '铁']] 17 ['yu', 'jie', 'fu', 'Rich', 'tian', ['金', '银', '铜', '铁']] 18 -----------------------succ----------------------- 19 ['yu', 'jie', 'liu', 'Rich', 'tian', ['金', '铂', '铜', '铁']] 20 ['yu', 'jie', 'fu', 'Rich', 'tian', ['金', '铂', '铜', '铁']] 21 -----------------------succ----------------------- 22 ''' 23 24 users2[2] = '刘' 25 users2[5][3] = 'Fe' 26 27 print(users) 28 print(users2) 29 30 #['yu', 'jie', 'liu', 'Rich', 'tian', ['金', '铂', '铜', 'Fe']] 31 #['yu', 'jie', '刘', 'Rich', 'tian', ['金', '铂', '铜', 'Fe']]
初步认识,浅copy 复制第一层的信息到变量中,两者独立,所以改变第一·层次的元素不会影响其他。列表中的列表为一个地址,复制地址到新变量中,地址的元素没有独立出去,所以改变后会影响新的变量
与copy模块里面copy相同
深copy
copy模块的deepcopy
可以复制全部内容,但可能造成内存浪费
列表循环:
for i in list:
print(i)
切片补:
>>> users = ['yu', 'jie', 'jie', 'fu', 'Rich', 'tian']
>>> users[1:6:2]
['jie', 'fu', 'tian']
最后一个为步长,和matlab有一定区别