首页 > 代码库 > Python-基础知识-列表list和元组tuple

Python-基础知识-列表list和元组tuple

列表List

创建列表:

>>> week = ["Sun","Mon","Tue","Wen","Thu","Fri","Sat"]
>>> week
[‘Sun‘, ‘Mon‘, ‘Tue‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘Sat‘]

基本操作:

  • 索引
  • 切片
  • 追加
  • 删除
  • 长度
  • 切片
  • 循环
  • 包含

一、索引

  用索引来访问list中每一个位置的元素,记得索引是从0开始,使用-1获取list中最后一个元素:

>>> week[0]
‘Sun‘
>>> week[-1]
‘Sat‘

 

二、切片

>>> print(week[0:3])    #切片
[‘Sun‘, ‘Mon‘, ‘Tue‘]
>>> week[3:-1]
[‘Wen‘, ‘Thu‘, ‘Fri‘]
>>> print(week[::2])    #带偏移切片
[‘Sun‘, ‘Tue‘, ‘Thu‘, ‘Sat‘]

 

三、追加

>>> week.append("One")
>>> week
[‘Sun‘, ‘Mon‘, ‘Tue‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘Sat‘, ‘One‘]

 

四、插入

>>> week.insert(1,"Two")    #在指定位置插入元素
>>> week
[‘Sun‘, ‘Two‘, ‘Mon‘, ‘Tue‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘Sat‘, ‘One‘]

 

五、修改

>>> week = ["Sun","Mon","Tue","Wen","Thu","Fri","Sat"]
>>> week
[‘Sun‘, ‘Mon‘, ‘Tue‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘Sat‘]
>>> week[1] = "Four"    #修改指定元素的值
>>> week
[‘Sun‘, ‘Four‘, ‘Tue‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘Sat‘]

 

六、删除

>>> week.remove("One")    #删除list中指定值的元素
>>> week
[‘Sun‘, ‘Four‘, ‘Mon‘, ‘Tue‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘Sat‘]
>>> del week[3]    #删除list中指定位置元素
[‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘Sat‘]
>>> week.pop()    #删除list中最后一个元素
‘Sat‘
>>> week
[‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘]

 

七、扩展

>>> Num = ["One","Two","Three","Four","Five","Six","Seven"]
>>> week.extend(Num)
>>> week
[‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘One‘, ‘Two‘, ‘Three‘, ‘Four‘, ‘Five‘, ‘Six‘, ‘Seven‘]

 

八、拷贝

>>> week2 = week.copy()    #深复制,id值不一样
>>> week
[‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘One‘, ‘Two‘, ‘Three‘, ‘Four‘, ‘Five‘, ‘Six‘, ‘Seven‘]
>>> week2
[‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘One‘, ‘Two‘, ‘Three‘, ‘Four‘, ‘Five‘, ‘Six‘, ‘Seven‘]
>>> id(week)
2608861285192
>>> id(week2)
2608861285896

>>> week2 = week    #浅复制,id值一样
>>> week
[‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘One‘, ‘Two‘, ‘Three‘, ‘Four‘, ‘Five‘, ‘Six‘, ‘Seven‘]
>>> week2
[‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘One‘, ‘Two‘, ‘Three‘, ‘Four‘, ‘Five‘, ‘Six‘, ‘Seven‘]
>>> id(week)
2608861285192
>>> id(week2)
2608861285192


#另一种复制方法
>>> import copy
>>> week2 = copy.deepcopy(week)
>>> week
[‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘One‘, ‘Two‘, ‘Three‘, ‘Four‘, ‘Five‘, ‘Six‘, ‘Seven‘]
>>> week2
[‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘One‘, ‘Two‘, ‘Three‘, ‘Four‘, ‘Five‘, ‘Six‘, ‘Seven‘]
>>> id(week)
2608861285192
>>> id(week2)
2608862744520
>>> week2 = copy.copy(week)
>>> week
[‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘One‘, ‘Two‘, ‘Three‘, ‘Four‘, ‘Five‘, ‘Six‘, ‘Seven‘]
>>> week2
[‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘One‘, ‘Two‘, ‘Three‘, ‘Four‘, ‘Five‘, ‘Six‘, ‘Seven‘]
>>> id(week)
2608861285192
>>> id(week2)
2608861304840

 

九、排序和翻转

>>> names
[‘Alex‘, ‘Tenglan‘, ‘Amy‘, ‘Tom‘, ‘Amy‘, 1, 2, 3]
>>> names.sort() #排序
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()   #3.0里不同数据类型不能放在一起排序了,擦
>>> names[-3] = ‘1‘
>>> names[-2] = ‘2‘
>>> names[-1] = ‘3‘
>>> names
[‘Alex‘, ‘Amy‘, ‘Amy‘, ‘Tenglan‘, ‘Tom‘, ‘1‘, ‘2‘, ‘3‘]
>>> names.sort()
>>> names
[‘1‘, ‘2‘, ‘3‘, ‘Alex‘, ‘Amy‘, ‘Amy‘, ‘Tenglan‘, ‘Tom‘]

>>> names.reverse() #反转
>>> names
[‘Tom‘, ‘Tenglan‘, ‘Amy‘, ‘Amy‘, ‘Alex‘, ‘3‘, ‘2‘, ‘1‘]

 

十、统计

>>> names
[‘Alex‘, ‘Tenglan‘, ‘Amy‘, ‘Tom‘, ‘Amy‘, 1, 2, 3]
>>> names.count("Amy")
2

 

十一、获取下标

>>> week
[‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘One‘, ‘Two‘, ‘Three‘, ‘Four‘, ‘Five‘, ‘Six‘, ‘Seven‘]
>>> week.index("Mon")    #返回第一个找到的元素的下标
2

 

元组Tuple

  元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表。

  创建元组,因为元组不能修改,它只有2个方法,一个是count,一个是index。

>>> week = (‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘One‘, ‘Two‘, ‘Three‘, ‘Four‘, ‘Five‘, ‘Six‘, ‘Seven‘)
>>> week
(‘Sun‘, ‘Four‘, ‘Mon‘, ‘Wen‘, ‘Thu‘, ‘Fri‘, ‘One‘, ‘Two‘, ‘Three‘, ‘Four‘, ‘Five‘, ‘Six‘, ‘Seven‘)
>>> week.index("Mon")
2
>>> week.count(‘Mon‘)
1

 

Python-基础知识-列表list和元组tuple