点击查看上一篇文章:Python笔记2 变量
一、列表 List列表是类似于C中数组的存在,用于存储一系列值。
names = ['Alice', 'Bob', 'Sam'] print (names)
['Alice', 'Bob', 'Sam']① 访问列表中的元素
当角标为-1时,访问末尾的元素。
当角标为0时,访问第一个元素。
print (names[0]) print (names[-1])
Alice Sam② 修改、添加、删除元素 1. 修改
names = ['Alice', 'Bob', 'Sam'] print (names[0]) names[0] = 'Herman' print (names[0])
Alice Herman2. 添加
在列表末尾添加元素:
list_name.append(element_to_add)
names = ['Alice', 'Bob', 'Sam']
print (names)
names.append('Herman')
print (names)
['Alice', 'Bob', 'Sam'] ['Alice', 'Bob', 'Sam', 'Herman']
在列表中插入元素:
number为该元素插入列表后,它的索引号。
插入过程中,该索引位置与索引位置之后的所有值均后移一位索引号。
list_name.insert(number, element_to_add)
names = ['Alice', 'Bob', 'Sam'] print (names) names.insert(2, 'Herman') print (names)
['Alice', 'Bob', 'Sam'] ['Alice', 'Bob', 'Herman', 'Sam']3. 删除
根据索引,删除列表中的一个元素并不再引用它:
del list_name[number]
names = ['Alice', 'Bob', 'Sam'] del names[0] print (names)
['Bob', 'Sam']
根据索引,删除元素并在另一处引用它:
pop是来自栈的概念,类似于将栈顶元素弹出。
在pop()中,number默认为-1,pop将默认弹出在列表尾部的单个元素,当指定元素索引号时,将pop指定元素。
poped_element = list_name.pop(number)
names = ['Alice', 'Bob', 'Sam', 'Herman'] poped_name_default = names.pop() print (names) print (poped_name_default) poped_name_select = names.pop(1) print (names) print (poped_name_select)
['Alice', 'Bob', 'Sam'] Herman ['Alice', 'Sam'] Bob
根据值,删除元素:
remove()只会删除找到的第一个对应的值,假设列表中存在多个这样的值,将需要借助循环来删除。
element_remove = value_of_element list_name.remove(element_remove)
names = ['Alice', 'Bob', 'Sam', 'Herman', 'Alice'] print (names) name_remove = 'Alice' names.remove(name_remove) print (names)
['Alice', 'Bob', 'Sam', 'Herman', 'Alice'] ['Bob', 'Sam', 'Herman', 'Alice']
借助循环删除对应值的所有元素:
names = ['Alice', 'Bob', 'Sam', 'Alice', 'Herman', 'Alice']
while 'Alice' in names:
names.remove('Alice')
print(names)
['Bob', 'Sam', 'Alice', 'Herman', 'Alice'] ['Bob', 'Sam', 'Herman', 'Alice'] ['Bob', 'Sam', 'Herman']③ 组织列表 1. 列表排序
使用sort()对列表实现永久排序:
当在sort()中添加reverse = True时,将会使当前列表永久逆排序。
list_name.sort()
names = ['Herman', 'Sam', 'Bob', 'Alice'] print (names) names.sort() print (names) names.sort(reverse = True) print (names)
['Herman', 'Sam', 'Bob', 'Alice'] ['Alice', 'Bob', 'Herman', 'Sam'] ['Sam', 'Herman', 'Bob', 'Alice']
使用sorted()对列表实现临时排序:
临时排序不会影响到列表本身的值与顺序,同样可以添加参数reverse=True进行逆排序。
sorted(list_name,reverse = True)
names = ['Herman', 'Sam', 'Bob', 'Alice'] print (names) print (sorted(names)) print (sorted(names,reverse = True)) print (names)
['Herman', 'Sam', 'Bob', 'Alice'] ['Alice', 'Bob', 'Herman', 'Sam'] ['Sam', 'Herman', 'Bob', 'Alice'] ['Herman', 'Sam', 'Bob', 'Alice']2. 反转列表
列表反转后,原来的列表最后一位元素将成为第一位元素,该命令不会进行排序。
list_name.reverse()
names = ['Herman', 'Sam', 'Bob', 'Alice'] print (names) names.reverse() print (names)
['Herman', 'Sam', 'Bob', 'Alice'] ['Alice', 'Bob', 'Sam', 'Herman']3. 获取列表长度
即获取列表中元素的个数。
len(list_name)
names = ['Herman', 'Sam', 'Bob', 'Alice'] len(names)
44. 列表切片
列表切片可以获得列表中的一部分片段。
切片将会获得由索引起始点到终止点前一位的元素,组成一个新列表。
从列表第一个元素到索引为n的元素前: list_name[:n]
从索引为n的元素到列表末尾: list_name[n:]
倒数n个元素: list_name[-n:]
list_name[index_start:index_end]
names = ['Herman', 'Sam', 'Bob', 'Alice', 'Hank'] print(names[0:3]) print(names[:2]) print(names[1:]) print(names[-2:])
['Herman', 'Sam', 'Bob'] ['Herman', 'Sam'] ['Sam', 'Bob', 'Alice', 'Hank'] ['Alice', 'Hank']5.复制列表
通过4中的切片方法,若将整个列表切片,则就相当于复制了整个列表。
如果直接使list_name_copy = list_name 会导致新列表变量关联到旧列表变量对应的列表,也就是两个变量指向同一个列表,得到错误的结果。
list_name_copy = list_name[:]
names = ['Herman', 'Sam', 'Bob', 'Alice', 'Hank'] names_copy = names[:] print(names_copy)
['Herman', 'Sam', 'Bob', 'Alice', 'Hank']④ 列表常用函数 1. 获取数字列表中元素的最大、最小值与总和
最大值:
max(list_name)
最小值:
min(list_name)
总和:
sum(list_name)
digits = [5, 14, 3, 12.5, 66, 195, 8] max(digits) min(digits) sum(digits)
195 3 303.5二、元组 Tuple
元组是不可修改类型的列表,仅在定义时有所区别
height = (1, 15, 23) print(height[0]) #Error height[0] = 5
1 Traceback (most recent call last): File "三、集合 Set", line 1, in TypeError: 'tuple' object does not support item assignment
集合类似于列表,但是每个元素必须独一无二,可以使用集合来剔除列表中重复的部分
set(list_name)
names = ['Herman', 'Alice', 'Bob','Alice', 'Alice', 'Herman'] for name in set(names): print(name)
Bob Herman Alice



