1. 列表(有序/可遍历/可删除/可插入/可修改/可以通过编号访问)访问list从左到右,从右到左
2. 列表定义scores=['zhd','xiaoming','xiaohong']查看类型 print(type(scores))
3. print(len(scores))根据索引取值,超出索引范围报错IndexError
4. del 索引时候索引必须存在,否则报错,len获取list的长度
5. 'zhd' in scores 判断字符串是否在scores这个列表 'zhd' not in scorse 判断是否不再
6. list (range(1,10,3)) start (1), stop(10) ,step(3) print([1, 4, 7])
求最大值
nums=[6,11,7,9,4,2,1]
heandle = 0 #开始值
for x in nums:
#循环x列表
if x > heandle:
#x列表的值和开始值比较
heandle = x
#开始值小的话就把x列表大的值赋值给heandle
print(heandle)
#############
求最小值
nums=[-6,-19,-7,-9,-4,-2,-1]
heander = None
for num in nums:
if heander is None:
heander=num
elif num >heander:
heander =num
print(heander)
切片 start,end,step
l = list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l[3:6]
[3, 4, 5]
#######
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l[2:8:2]
[3, 5, 7]
#######
l1=[1,2,3,4,5,6,7,8,9]
l2=l1
l3=l1[:]
l1[0]=100
print(l1)
print(l2)
print(l3)
#appad函数把整体追加到最后
l = ['a','b','c']
l.append('d')
['a', 'b', 'c', 'd']
#copy函数
l = list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l2=l.copy
id(l),id(l2)
(4506220040, 4477604128)
# l.count统计出现的次数
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 1, 1, 1]
l.count(1)
1出现了4 次
#l.extend('zhd') 扩展列表把整体分开追加
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 1, 1, 1, 'z', 'h', 'd']
#l.index('d') 获取索引位置
#help(list.insert) 插入指定位置
l.insert(0,'zhd')
#l.pop(0) 移除并返回值
#l.remove(1)移除不返回结果
#l.reverse() 反转
#l.sort() 排序 从小到大排列
#l.clear 清空list的元素
#dir(list) 查看有哪些数据类型
7.列表求交际
l1 = [1,4,6,7,9,9]
l2 = [1,1,4,9,9,8]
l3 = []
for x in l1:
for j in l2:
if x == j and x not in l3:
l3.append(x)
print(l3)
8.元组不可变的列表,只读的列表,不能修改不能删除
9.元组是有括号圈起来的如果只有一个元素必须有逗号例如t1=(1,)
10.元组根据索引来访问,元组是可迭代对象,元组里面尽量不要去存list容易误导
tuple('abcdefg')
('a', 'b', 'c', 'd', 'e', 'f', 'g')
11. 元组的应用场景如下:
不希望被修改例如一年12个月
month_tuple=('一月','二月',...,'十二月')
('姓名','年龄','电话','地址')