列表
1.列表的创建
list = [1,1.2,True,'westos']
print(list,type(list))
列表里也可以嵌套列表
list2 = [1,2,3,4,[1,1.2,True,'westos']]
print(list2,type(list2))
2.列表的特性
service = [‘http’,‘ftp’,‘ssh’]
索引: 正向索引
print(service[0])
反向索引
print(service[-1])
切片
print(service[::-1])
print(service[1:])
print(service[:-1])
重复
print(service * 3)
成员操作符
print('firewalld' in service)
print('ftp' not in service)
== for循环==
for i in service:
print(i)
3.列表元素的增加
service = [‘http’,‘ftp’,‘ssh’]
append():追加一个元素到列表
service.append('firewalld')
print(service)
extend():拉伸 追加多个元素到列表
service.extend(['mysql','nfs'])
print(service)
insert():在指定索引处插入元素
service.insert(1,'dns')
print(service)
4.列表元素的删除

service = ['http','ftp','ssh']
a = service.remove('ftp')
print(service)
print(a)
5.列表元素的查看
service = [‘http’,‘ftp’,‘ssh’,‘mysql’,‘ssh’,‘http’]
查看元素在列表中出现的位置
print(service.count('ssh'))
查看指定元素的索引值(可以指定搜索范围)
print(service.index('ssh'))
print(service.index('ssh',4,7))
6.列表元素的排序
import random
service = ['ftp','ssh','http','mysql','http','ssh']
# 默认按照ascii码进行排序的
service.sort(reverse=True)
print(service)
li = list(range(0,101))
print(li)
random.shuffle(li)
print(li)
7.列表元素的修改
service = ['https','ftp','ssh','mysql']
# 通过索引,重新赋值
service[0] = 'http'
print(service)
# 通过切片赋值
service[:2] = ['samba','dns','firewalld']
print(service)
8.练习
names = [‘fentiao’, ‘fendai’, ‘fensi’, ‘apple’]
输出结果为:‘I have fentiao, fendai, fensi and apple.’

题目:输入某年某月某日(yyyy-MM-dd),判断这一天是这一年的第几天?

问题描述:按照下面的要求实现对列表的操作:
产生一个列表,其中有 40 个元素,每个元素是 50 到 100 的一个随机整数
如果这个列表中的数据代表着某个班级 40 人的分数,请计算成绩低于平均分的学生人数
对上面的列表元素从大到小排序并输出li.sort(reverse=True)

693

被折叠的 条评论
为什么被折叠?



