python学习6:列表


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.列表的特性

(1)索引
list[num]:正向索引
list[-num]:反向索引

service = ['http','ftp','ssh']
print(service[0])
print(service[-1])

在这里插入图片描述
(2)切片
list[::-1]:翻转
list[1:]:列表中除去第一个元素的其他元素
list[:-1]:列表中除去最后一个元素的其他元

service = ['http','ftp','ssh']
print(service[::-1])
print(service[1:])
print(service[:-1])

在这里插入图片描述
(3)重复
list * 3:重复打印列表3遍

service = ['http','ftp','ssh']
print(service * 3)

在这里插入图片描述
(4)成员操作符

service = ['http','ftp','ssh']
print('firewalld' in service)
print('ftp' not in service)

在这里插入图片描述
(5)连接
list + list1:连接列表list与列表list1,形成新列表

service = ['http','ftp','ssh']
service1 = ['mysql','firewalld']
print(service + service1)

在这里插入图片描述

(6)for循环

service = ['http','ftp','ssh']
for i in service:
    print(i)

在这里插入图片描述
练习1:

假定有下面这样的列表:
names = [‘fentiao’, ‘fendai’, ‘fensi’, ‘apple’]
输出结果为:'I have fentiao, fendai, fensi and apple.

names = ['fentiao', 'fendai', 'fensi', 'apple']
name1 = names[:3]
name11=','.join(name1)
name2 = names[3]
print('i have %s and %s' % (name11, name2))

在这里插入图片描述
练习2:

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

date = input('输入年月日(yyyy-mm-dd):')
date1 = date.split('-')
# print(date1)
day = int(date1[2])
month = int(date1[1])
year = int(date1[0])
month_day = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    # print('此年份为闰年')
    month_day[2] = 29
    num_day = 0
    for i in range(0, month):
        num_day += month_day[i]
    num_day = num_day + day
else:
    num_day = 0
    for i in range(0, month):
        num_day += month_day[i]
    num_day = num_day + day
print(num_day)

在这里插入图片描述

3.列表的常用方法

(1)列表元素的增加
list.append():追加一个元素到列表
list.extend():拉伸 追加多个元素到列表
list.insert():在指定索引处插入元素

service = ['http','ftp','ssh']
service.append('firewalld')
print(service)

在这里插入图片描述

service = ['http','ftp','ssh']
service.extend(['mysql','nfs'])
print(service)

在这里插入图片描述

service = ['http','ftp','ssh']
service.insert(1,'dns')
print(service)

在这里插入图片描述
(2)列表元素的删除
pop():弹出列表最后一个元素
remove():删除列表元素,从内存中删除
del:从内存中删除一个元素,del可删除变量

>>> service = ['http','ftp','ssh']
>>> service.pop()
'ssh'
>>> service
['http', 'ftp']
service = ['http','ftp','ssh']
a = service.remove('ftp')
print(service)
print(a)

在这里插入图片描述

service = ['http','ftp','ssh']
del service[1]
print(service)

在这里插入图片描述
(3)列表元素的查看

service = ['http','ftp','ssh','mysql','ssh','http']
# 查看元素在列表中出现的次数
print(service.count('ssh'))
# 查看指定元素的索引值(可以指定搜索范围)
print(service.index('ssh'))
print(service.index('ssh',4,7))

在这里插入图片描述
(4)列表元素的排序

service = ['ftp','ssh','http','mysql','http','ssh']
# 默认按照ascii码进行排序的
service.sort(reverse=True)
print(service)

在这里插入图片描述

import random
li = list(range(0,10))
print(li)
random.shuffle(li)  ##讲顺序打乱
print(li)

在这里插入图片描述
练习1:

有一个列表,其中包括 10 个元素,例如这个列表是[1,2,3,4,5,6,7,8,9,0]

  • 例如这个列表是[1,2,3,4,5,6,7,8,9,0],
  • 第一个元素到列表的最后,然后输出这个列表。
  • 最终样式是[2,3,4,5,6,7,8,9,0,1]

a.pop(index):删除列表a中index处的值,并且返回这个值.
del(a[index]):删除列表a中index处的值,无返回值. del中的index可以是切片,所以可以实现批量删除.
a.remove(value):删除列表a中第一个等于value的值,无返回.

list=input('请输入10个元素(eg:1,2,3,4,5,6,7,8,9,0):')
list2=list.split(',')
list2_0=list2.pop(0)
list2.append(list2_0)
print(list2)

在这里插入图片描述

练习2:

问题描述:按照下面的要求实现对列表的操作:

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

sum(list):对列表求和
len(list):求列表的长度

grade_all = []
grade1 = 0
for i in range(40):
    grade = random.randint(50, 100)
    grade_all.append(grade)
    grade1 += grade
precentage = grade1 / 40
print('平均成绩为:%.f' % (precentage))
n = 0
for i in grade_all:
    if i < precentage:
        n += 1
print('低于平均成绩的人数是:%d' % (n))
grade_all.sort(reverse=True)
print(grade_all)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值