003-python列表

时间:2023-02-07 09:24:35

Python 列表(list)

列表是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。

列表的基本操作:

  1. 索引
  2. 切片
  3. 追加
  4. 删除
  5. 长度
  6. 循环
  7. 包含

查看列表有哪些方法

>>> a = [1,2,3,4,]
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

定义一个列表namelist1

>>> namelist1 = ["lilei", "hanmeimei", "lixue", "xiaoming"]
>>> namelist1
['lilei', 'hanmeimei', 'lixue', 'xiaoming']

1.通过下标访问列表中的元素,下标从0开始:

>>> namelist1[0]
'lilei'
>>> namelist1[1]
'hanmeimei'

2.切片,取多个元素

>>> namelist1[0:3]
['lilei', 'hanmeimei', 'lixue']

3.追加:

>>> namelist1
['lilei', 'hanmeimei', 'lixue', 'xiaoming']
>>> namelist1.append("Jack")
>>> namelist1
['lilei', 'hanmeimei', 'lixue', 'xiaoming', 'Jack']

4.指定位置插入,将Long插入到第二个下标位置:

>>> namelist1.insert(2, "Long")
>>> namelist1
['lilei', 'hanmeimei', 'Long', 'lixue', 'xiaoming', 'Jack']

5.修改,将下标2的Long修改为LONG:

>>> namelist1
['lilei', 'hanmeimei', 'Long', 'lixue', 'xiaoming', 'Jack']
>>> namelist1[2] = "LONG"
>>> namelist1
['lilei', 'hanmeimei', 'LONG', 'lixue', 'xiaoming', 'Jack']

6.删除

6.1指定下标删除"del":
>>> namelist1
['lilei', 'hanmeimei', 'LONG', 'lixue', 'xiaoming', 'Jack']
>>> del namelist1[2]
>>> namelist1
['lilei', 'hanmeimei', 'lixue', 'xiaoming', 'Jack']
6.2指定元素删除"remove":
>>> namelist1
['lilei', 'hanmeimei', 'lixue', 'xiaoming', 'Jack']
>>> namelist1.remove("lixue")
>>> namelist1
['lilei', 'hanmeimei', 'xiaoming', 'Jack']
6.3删除列表最后一个值:
>>> namelist1
['lilei', 'hanmeimei', 'xiaoming', 'Jack']
>>> namelist1.pop()
'Jack'
>>> namelist1
['lilei', 'hanmeimei', 'xiaoming']

7.将其他列表list2合并到当前列表:

>>> list2 = [1,2,3,4]
>>> list2
[1, 2, 3, 4]
>>> namelist1.extend(list2
list2
>>> namelist1.extend(list2)
>>> namelist1
['lilei', 'hanmeimei', 'xiaoming', 1, 2, 3, 4]

8.统计(count)列表中存在几个相同的值“Jack”:

>>> namelist1.insert(2,"Jack")
>>> namelist1.insert(2,"Jack")
>>> namelist1.insert(2,"Jack")
>>> namelist1.insert(-1,"Jack")
>>> namelist1
['lilei', 'hanmeimei', 'Jack', 'Jack', 'Jack', 'xiaoming', 1, 2, 3, 'Jack', 4]
>>> namelist1.count("Jack")
4

9.排序&翻转排序(sort & reverse)

注:Python3.0里不同数据类型不能放在一起排序(字符和数字不能一同排序)

9.1按照ASCII排序
>>> namelist1
['Jack', 'Jack', 'Jack', 'hanmeimei', 'lilei', 'xiaoming', '5', '4', '3', 'Jack', '4']
>>> namelist1.sort()
>>> namelist1
['3', '4', '4', '5', 'Jack', 'Jack', 'Jack', 'Jack', 'hanmeimei', 'lilei', 'xiaoming']
9.2翻转排序:
>>> namelist1.reverse()
>>> namelist1
['xiaoming', 'lilei', 'hanmeimei', 'Jack', 'Jack', 'Jack', 'Jack', '5', '4', '4', '3']

10.获取列表中值的下标(index):

注:默认只获取第一个下标

['xiaoming', 'lilei', 'hanmeimei', 'Jack', 'Jack', 'Jack', 'Jack', '5', '4', '4', '3']
>>> namelist1.index("hanmeimei")
2
>>> namelist1.index("Jack")
3

11.统计一共有几个值:

>>> namelist1
['xiaoming', 'lilei', 'hanmeimei', 'Jack', 'Jack', 'Jack', 'Jack', '5', '4', '4', '3']
>>> len(namelist1)
11

12.去除列表值中的前后空格(strip)

注:默认去除空格

>>> namelist1.append("xxxx   ")
>>> namelist1
['xiaoming', 'lilei', 'hanmeimei', 'Jack', 'Jack', 'Jack', 'Jack', '5', '4', '4', '3', 'xxxx ']
>>> namelist1[-1]
'xxxx '
>>> namelist1[-1].strip()
'xxxx'

13.统计列表一共有几"Jack"相同的元素(count):

>>> namelist1
['xiaoming', 'lilei', 'hanmeimei', 'Jack', 'Jack', 'Jack', 'Jack', '5', '4', '4', '3']
>>> namelist1.count("Jack")
4

14.将列表依照指定符号分隔(join):

>>> namelist1
['xiaoming', 'lilei', 'hanmeimei', 'Jack', 'Jack', 'Jack', 'Jack', '5', '4', '4', '3']
>>> "***".join(namelist1)
'xiaoming***lilei***hanmeimei***Jack***Jack***Jack***Jack***5***4***4***3'
>>> "*".join(namelist1)
'xiaoming*lilei*hanmeimei*Jack*Jack*Jack*Jack*5*4*4*3'
>>> "-".join(namelist1)
'xiaoming-lilei-hanmeimei-Jack-Jack-Jack-Jack-5-4-4-3'