文章目录
增删排序都需要输入原来的变量,不能直接输入更改后的,不需要其他变量接收。
1 列表的定义
数组:用于存储多个相同类型数据的集合。
列表:pyhton没有数组。列表是打了激素的数组,可以存储任意数据类型。
2 列表的创建
list = [1, 1,2, 'westos', [1,2.2,'we'],True]
print(list,type(list))
结果是:
[1, 1, 2, 'westos', [1, 2.2, 'we'], True] <class 'list'>
3 列表的特性
3.1 索引
service = ['http','ftp','ssh']
print(service[0])
print(service[-1])
print(service[2])
结果是:
http
ssh
ssh
3.2 切片
service = ['http','ftp','ssh']
print(service[:1])
print(service[:-1])
print(service[1:])
print(service[::-1])
结果是:
['http']
['http', 'ftp']
['ftp', 'ssh']
['ssh', 'ftp', 'http']
3.3 重复
service = ['http','ftp','ssh']
print(service*3)
结果是:
['http', 'ftp', 'ssh', 'http', 'ftp', 'ssh', 'http', 'ftp', 'ssh']
3.4 连接
service = ['http', 'ftp', 'ssh']
people = ['mysql', 'firewalld']
print(service + people)
结果是:
['http', 'ftp', 'ssh', 'mysql', 'firewalld']
3.5 成员操作符
service = ['http', 'ftp', 'ssh']
people = ['mysql', 'firewalld']
print('ftp' in service)
print('ftp' not in service)
结果是:
True
False
3.6 for循环
service = ['http', 'ftp', 'ssh']
for i in service:
print(i)
结果是:
http
ftp
ssh
4 列表的常用方法
4.1 列表元素的增加(必须print(原来的变量))
list.append():追加一个元素到列表
list.extend():拉伸,追加多个元素
list.insert():在指定索引处插入元素
service = ['http', 'ftp', 'ssh']
service.append('hhh')
print(service)
service.extend(['mysql', 'gd'])
print(service)
service.insert(1, 'dns')
print(service)
结果是:
['http', 'ftp', 'ssh', 'hhh']
['http', 'ftp', 'ssh', 'hhh', 'mysql', 'gd']
['http', 'dns', 'ftp', 'ssh', 'hhh', 'mysql', 'gd']
4.2 列表元素的删除
pop():弹出列表最后一个元素
remove():删除列表元素
del:从内存中删除一个元素,del可删除变量
service = ['http', 'ftp', 'dns']
a = service.pop()
print(a)
print(service)
b = service.remove('ftp')
print(b)
print(service)
del service[0]
print(service)
结果是:
dns
['http', 'ftp']
None
['http']
[]
4.3 列表元素的个数与索引值
service = ['http', 'ssh','ftp', 'dns','ssh']
print(service.count('ssh'))
print(service.index('ssh'))
print(service.index('ssh',3,5))
结果是:
2
1
4
4.4 列表元素的排序(默认ASCII)
service = ['http', 'ssh', 'ftp', 'dns', 'ssh']
service.sort()
print(service)
service.sort(reverse=True)
print(service)
结果是:
['dns', 'ftp', 'http', 'ssh', 'ssh']
['ssh', 'ssh', 'http', 'ftp', 'dns']
数字排序:
li = list(range(0, 101))
print(li)
import random
random.shuffle(li)
print(li)
结果是:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
[85, 95, 5, 71, 27, 48, 44, 13, 98, 53, 77, 72, 42, 73, 51, 17, 55, 61
5 列表之练习
练习1:
name = [‘fentiao’, ‘fendai’, ‘fensi’, ‘apple’],输出结果为:I hava fentiao,fendai,fensi and apple
name = ['fentiao', 'fendai', 'fensi', 'apple']
name1 = name[:-1]
name2 = name[-1]
print('I hava ' + ','.join(name1) + ' and ' + name2)
print('I hava ' + ','.join(name[:-1]) + ' and ' + name[-1])
结果是
I hava fentiao,fendai,fensi and apple
I hava fentiao,fendai,fensi and apple
练习2:
输入某年月日,判断这一天是这一年的第几天.
cal = input('请输入格式为yyyy--mm--dd的年月日:')
date = cal.split('-')
print(date)
year = int(date[0])
month = int(date[1])
day = int(date[2])
arr =[0,31,28,31,30,31,30,31,31,30,31,30,31]
num = 0
if ((year%4==0 and year%100 !=0)or year%400==0):
arr[2]=29
for i in range(1,len(arr)):
if month>i:
num+=arr[i]
else:
num+=day
break
print('第%d天'%(num))
结果是:
请输入格式为yyyy--mm--dd的年月日:2020-3-4
['2020', '3', '4']
第64天
练习3:一个包含10个元素的列表如【1,2,3,4,5,6,7,8,9,0】
要求将每个元素依次向前移动一个位置,第一个元素到最后,输出【2,3,4,5,6,7,8,9,0,1,】
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
b = a.pop(0)
print(b)
a.append(b)
print(a)
结果是:
1
[2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
练习4:
有40个(50-100)的列表,代表40个人的分数,计算成绩低于平均分的学生人数。列表从大到小排序。
import random
score = []
avg = 0
count = 0
for i in range(40):
score.append(random.randint(50, 100))
avg = sum(score) / 40
score.sort(reverse=True)
for i in score:
if i < avg:
count += 1
print('平均成绩是%.2f\n低于平均成绩人数为%d\n排序结果为%s'%(avg, count,score))
结果是:
平均成绩是74.62
低于平均成绩人数为21
排序结果为[99, 99, 98, 97, 97, 95, 95, 94, 92, 83, 82, 82, 80, 80, 80, 77, 77, 76, 76, 74, 74, 72, 72, 69, 68, 68, 67, 67, 67, 66, 66, 65, 64, 55, 55, 53, 52, 51, 51, 50]