一、定义
定义列表的方式是[]
1、用列表存储了3个学生的姓名
stu_names = [‘wanglaoshi’, ‘dongge’, ‘dong4716138’]
2、用列表存储了5个分数
scores = [100, 99, 100, 95, 90]
3、用列表存储了2个分数和2个字符串
scores = [100, 99,‘sad’,“sadasd”]
print(scores)
和c语言差别
相同点:列表和数组都能实现多个数据的存储
不同点:列表可以存储不同类似的数据,而数组只能存储相同数据类型。
虽然Python中的列表可以存储多个不同的数据类型,但一般情况下我们很少这么做,而是把数据类型相同的数据放到一个列表,这样操作时会更加方便,否则就会出现一会整数,一会字符串很容易出错,
如果真的需要存储多个数据,例如一个人的信息由多个组成,此时一般用Python中的字典来进行存储
二、下标
列表和c语言的数组类似,也是可以用下标来控制
print(scores[0])
三、切片
列表也是支持切片
scores = [100, 99,'sad',"sadasd"]
a=scores[1:3] # 此时得到一个新列表
print(a)
>>>
[99, 'sad']
四、遍历
使用for、while即可
stu_names = ['wanglaoshi', 'dongge', 'dong4716138']
for name in stu_names:
print(name)
stu_names = ['wanglaoshi', 'dongge', 'dong4716138']
length = len(stu_names)
i = 0
while i<length:
print(stu_names[i])
i+=1
五、列表相关操作
- [1 ] append
通过append可以向列表添加元素
stu_names = ['a1', 'a2', 'a3']
for numeber in stu_names:
print(numeber)
stu_names.append('a4')
for numeber in stu_names:
print(numeber)
- [2 ] extend
通过extend可以将另一个列表中的元素逐一添加到列表中
stu_names = ['a1', 'a2', 'a3']
stu_names2 = ['b1', 'b2', 'b3']
for numeber in stu_names:
print(numeber,end=" ")
stu_names.extend(stu_names2)
print("\n")
for numeber in stu_names:
print(numeber,end=" ")
- [3 ] insert
insert(index, object) 在指定位置index(索引,理解为下标即可)前插入元素object
stu_names = ['a1', 'a2', 'a3']
for numeber in stu_names:
print(numeber,end=" ")
stu_names.insert(1,"xx")
print("\n")
for numeber in stu_names:
print(numeber,end=" ")
- [4 ] 修改元素
列表[下标] = 新数据
查找元素(in, not in,)
in(存在),如果存在那么结果为true,否则false
not in(不存在),如果不存在那么结果为true,否则false
stu_names = ['a1', 'a2', 'a3']
finddata = "11"
for numeber in stu_names:
print(numeber)
if finddata in stu_names:
print("11存在")
else:
print("11不存在")
if finddata not in stu_names:
print("11不存在")
else:
print("11存在")
- [ 5] count 查个数
stu_names = ['a1', 'a2', 'a3', 'a1']
for numeber in stu_names:
print(numeber)
print( stu_names.count("a1") )
- [ 6] 删除(del, pop, remove)
del:根据下标进行删除
pop:删除最后一个元素
remove:根据元素的值进行删除
stu_names = ['a1', 'a2', 'a3']
for numeber in stu_names:
print(numeber,end=" ")
del stu_names[0]
print("\n")
for numeber in stu_names:
print(numeber,end=" ")
stu_names.pop()
print("\n")
for numeber in stu_names:
print(numeber,end=" ")
stu_names.remove("a2")
print("\n")
for numeber in stu_names:
print(numeber,end=" ")
- [ 7] 排序(sort, reverse)
sort方法是将列表按特定顺序重新排列,默认为由小到大,参数reverse=True可改为倒序,由大到小。reverse方法是将列表倒叙
stu_names = ['a6', 'a1', 'a3','a4']
for numeber in stu_names:
print(numeber,end=" ")
print("\n")
stu_names.sort()
for numeber in stu_names:
print(numeber,end=" ")
print("\n")
stu_names.sort(reverse=True)
for numeber in stu_names:
print(numeber, end=" ")
print("\n")
stu_names.reverse()
for numeber in stu_names:
print(numeber, end=" ")
六、列表嵌套
个列表中的元素又是一个列表,那么这就是列表的嵌套
school_names = [
['北京大学', '清华大学'],
['南开大学', '天津大学', '天津师范大学'],
['山东大学', '中国海洋大学']
]
import random
offices = [[],[],[]]
names = ['a','b','c','d','e','f','g','h']
for name in names:
random_num=random.randint(0,2)
offices[random_num].append(name)
i=1
for offices_name in offices:
print('办公室%d的人数为%d'%(i,len(offices_name)))
i+=1
for name in offices_name:
print(name)
print("\n")