当前位置: 移动技术网 > IT编程>脚本编程>Python > Python--列表学习及练习

Python--列表学习及练习

2020年07月20日  | 移动技术网IT编程  | 我要评论

引言

  我们先来思考这样一个问题:存储5个人的年龄,求他们的平均年龄。很简单吧,用下面的几行程序就可以完成。

age1 = 18
age2 = 19
age3 = 20
age4 = 21
age5 = 22
print((age1+age2+age3+age4+age5) / 5)

  但是要是我们需要算很多人的平均年龄该怎么办?
  思考一下假如我们要算100个人的平均年龄·,难道我们要创建100个变量来储存他们的年龄吗?
  解决方法:列表【是一种有序的集合】。

一、创建列表

格式:列表名 = [列表选项1,列表选项2,……,列表选项n]

(一)创建一个空列表

list1 = []
print(list1)  #编译结果:[]

(二)创建带有元素的列表

list2 = [18, 19, 20, 21, 22]
index = 0
sum = 0
while index < 5:
    sum += list2[index]
    index += 1
    if index == 5:
        print("平均年龄:%d" % (sum / 5)) 
#编译结果  平均年龄:20

(三)注意

  列表中的元素数据可以是不同类型的。

list3 = [1, 2, " no", " pains", True]
print(list3)
#编译结果:[1, 2, ' no', ' pains', True]

二、列表元素的访问

  注意不要越界(下标超出了可表示的范围)

(一)格式

格式:列表名[下标]

list4 = [1, 2, 3, 4, 5]
print(list4[2])  
#编译结果:3

(二)替换列表元素

list4[2] = 300
print(list4)
#编译结果:[1, 2, 300, 4, 5]

三、列表操作

(一)列表组合

  列表相加:

list5 = [1,2,3]
list6 = [4,5,6]
list7 = list5 + list6
print(list7)
#编译结果:[1, 2, 3, 4, 5, 6]

(二)列表的重复

list8 = [1,2,3]
print(list8 * 3) 
#编译结果:[1, 2, 3, 1, 2, 3, 1, 2, 3]

(三)判断元素是否在列表中

  使用成员运算符:in和not in

list9 = [1,2,3,4,5]
print(3 in list9)#编译结果:True
print(6 in list9)#编译结果:False

(四)列表截取

list10 = [1,2,3,4,5,6,7,8,9]
print(list10[2:6])#编译结果:[3, 4, 5, 6]
print(list10[3:])#编译结果:[4, 5, 6, 7, 8, 9]
print(list10[:5])#编译结果:[1, 2, 3, 4, 5]

(五)二维列表

list11 = [[1,2,3],[4,5,6],[7,8,9]]
print(list11[1])#编译结果:[4, 5, 6]
print(list11[1][1])#编译结果:5

四、列表方法

(一)list.append(obj):在列表中末尾添加新的元素

参数

  obj – 添加到列表末尾的对象。

例子

list12 = [1,2,3,4,5]
list12.append(6)
list12.append([7,8,9])
print(list12)
#编译结果:[1, 2, 3, 4, 5, 6, 6, [7, 8, 9]]

(二)list.extend(seq):在末尾一次性追加另一个列表中的多个值

参数

  seq – 元素列表。

例子

list13 = [1,2,3,4,5]
#list13.extend(6)  #会报错,因为添加的不是列表
list13.extend([6,7,8])
print(list13) 
#编译结果:[1, 2, 3, 4, 5, 6, 7, 8]

(三)list.insert(index, obj):在下标处添加一个元素,不覆盖原数据,原数据向后顺延

参数

  index – 对象 obj 需要插入的索引位置。
  obj – 要插入列表中的对象。

例子

list14 = [1,2,3,4,5]
list14.insert(2, 100)
print(list14) #[1, 2, 100, 3, 4, 5]
list14.insert(2, [200,300])
print(list14) #编译结果:[1, 2, [200, 300], 100, 3, 4, 5]

(四)list.pop(x=list[-1])

移除列表中指定下标出的元素(默认移除最后一个元素),并返回删除的数据

  1.探索list[-1]是多少?

list15 = [1,2,3,4,5]
print(list15[-1])  
#编译结果:5

  2.

list15 = [1,2,3,4,5]
list15.pop() #默认移除最后一个元素5   
print(list15)#编译结果:[1, 2, 3, 4]

  3.继续举例:去掉列表中的3

list15 = [1,2,3,4,5]
list15.pop(2)
print(list15) #编译结果:[1, 2, 4, 5]

  4.

list15 = [1,2,3,4,5]
print(list15.pop(1)) 
#返回了已经删除了的2
#编译结果:2

(五)list.remove():移除列表中的某个元素,若要移除列表中指定的元素有多个,那么移除第一个匹配的结果

  ①移除列表中指定的元素只有一个

list16 = [1,2,3,4]
list16.remove(4)#移除4
print(list16)#编译结果:[1, 2, 3]

  ②移除列表中指定的元素有多个

list16 = [1,2,3,4,5,4,5,4]
list16.remove(4)#只移除第一个匹配的结果
print(list16)#编译结果[1, 2, 3, 5, 4, 5, 4]

(六)list.clear():清除列表中所有的数据

list17 = [1,2,3,4,5]
list17.clear()
print(list17) #编译结果:[]

(七)list.index(x[, start[, end]]):从列表中找出某个值第一个匹配的索引值(该值所在的下标)

参数

  x-- 查找的对象。
  start-- 可选,查找的起始位置。
  end-- 可选,查找的结束位置。

例子

list18= [1,2,3,4,5]
index18 = list18.index(3)#3的下标为2
print(index18) #编译结果:2


list18= [1,2,3,4,5,3]
index18 = list18.index(3)#有两个3,只需找到第一个匹配的索引值即可
print(index18) #编译结果:2


list18= [1,2,3,4,5,3,4,5,6]
index18 = list18.index(3)
#圈定范围[4-7]  寻找指定范围内3的下标
index19 = list18.index(3, 4, 7)
print(index18, index19) #编译结果:2 5

(八)len(list):查看列表中的元素个数

参数

  list – 要计算元素个数的列表

例子

list20 = [1,2,3,4,5]
print(len(list20)) #编译结果:5

(九)max(list):获取列表中的最大值

参数

  list – 要返回最大值的列表。

例子

list21 = [1,2,3,4,5]
print(max(list21)) #编译结果:5

(十)min(list):获取列表中的最小值

参数

  list – 要返回最小值的列表。

例子

list22 = [1,2,3,4,5]
print(min(list22))#编译结果:1

(十一)list.count(obj):查看元素在列表中出现的次数

参数

  obj – 列表中统计的对象。

例子

list23 = [1,2,3,4,5,3,4,5,3,3,5,6]
print(list23.count(3))  #编译结果:4

(十二)list.reverse():列表倒序

list25 = [1,2,3,4,5]
list25.reverse()
print(list25) 
#编译结果:[5, 4, 3, 2, 1]

(十三)list.sort():列表排序

list26 = [2,1,3,5,4]
list26.sort()
print(list26)  
#编译结果:[1, 2, 3, 4, 5]

五、练习

(一)去除列表元素

  将一个列表中的一个元素全部去除

num = 0
list1 = [1,2,3,4,5,3,4,5,3,3,5,6]
all = list1.count(3)
while num < all:
    list1.remove(3)
    num += 1
print(list1)
#编译结果:[1, 2, 4, 5, 4, 5, 5, 6]

(二)时间下一秒

  逻辑:
    1.在控制台上输入一个时间字符串。
    2.用split方法,将时间字符串以冒号分割成三个字符串。
    3.将对应的字符串转成时,分,秒。
    4.随后根据秒满60,分进一,分满60,时进一。当时等于24时,变为0的逻辑来编写程序。
    5.输出

timeStr = input()  #接收一个时间字符串
#12:23:23

timeList = timeStr.split(":")  #将该字符串以冒号切割  ['12', '23', '23']
#print(timeList)

h = int(timeList[0])
m = int(timeList[1])
s = int(timeList[2]) #将切割后列表里每一个字符串转成时分秒

s += 1

if s == 60:  #秒满60 分加一
    m += 1
    s = 0
    if m == 60: #分满60 时加一
        h += 1
        m = 0
        if h == 24:
            h = 0
print("%.2d:%.2d:%.2d" %(h, m, s))

本文地址:https://blog.csdn.net/milu_yu/article/details/107142998

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网