列表List_python学习笔记

1 列表的介绍

1.1 单层列表

  • 列表用方括号[],元组用小括号()
  • 一个列表里的数据项,可以具备不同的数据类型
test_list = ['google', 1, 2, 'tom']
print(test_list) # 打印出来是['google', 1, 2, 'tom'],可见列表中可以有不同类型的数据

1.2 嵌套列表

list_1 = [1, 2, 3, 4]
list_2 = ['a', 'b', 'c']
list_new = [list_1, list_2]
print(list_new) # [[1,2,3,4], ['a', 'b', 'c']]
print(list_new[0]) # [1,2,3,4]
print(list_new[1]) # ['a', 'b', 'c']

2 列表的获取类方法

2.1 使用下标index,访问列表里的值

  • 使用下标访问列表的值,用list[index]
  • 访问列表的一个范围的值,用list[index1:index2]
test_list = ['google',1, 3, 5, 123, 53, 'tom']
# 获取指定下标的值
print(test_list[0]) # google
print(test_list[2]) # 3
# 访问列表的一个范围的值
print(test_list[3:6]) # 打印[5, 123, 53],此时是从index=3到index=5,也就是3到6的左臂右开区间

2.2 len获取列表的长度

  • len(list)获取列表list的长度
test_list = ['google',1, 3, 5, 123, 53, 'tom']
print(len(test_list)) # 7

2.3 获取列表的最大值、最小值

  • max(list) 获取列表list中的最大值
  • min(list) 获取列表list中的最小值
test_list = [1, 3, 5, 123, 53]
print(max(list)) # 123
print(min(list)) # 1 

2.4 统计元素出现的次数

  • 用list.count(x)来统计元素x在list中出现的次数
test_list = [10, 20, 30, 10, 11, 10]
print(test_list.count(10)) # 打印3

2.5 获取元素第一次出现的位置

  • list.index(x)获取元素x在list中第一次出现的位置index
test_list = [10, 20, 30, 10, 11, 10]
print(test_list.index(30)) # 打印2

3 列表的操作类方法

3.1 添加单个元素

  • 列表list添加元素x,用list.append(x)
test_list = [10, 20, 30]
test_list.apppend(50)
print(test_list) # [10, 20, 30, 50]

3.2 添加一个列表

  • 方式1.用newlist=list1+list2的方式,给list1添加list2
test_list1 = [10, 20, 30]
test_list2 = ['a', 'b', 'c']
new_list = test_list1+test_list2
print(type(new_list)) # <class 'list'> 合并后,类型还是list
print(new_list) # [10, 20, 30, 'a', 'b', 'c']
  • 方式2.用list1.extend(list2)
test_list1 = [10, 20, 30]
test_list2 = ['a', 'b', 'c']
test_list1.extend(test_list2)
print(test_list1) # [10, 20, 30, 'a', 'b', 'c']

3.3 更新元素

  • 更新列表list的index位置的元素,用list[index] = 新的值
test_list = [10, 20, 30]
test_list[1] = 100
print(test_list) # [10, 100, 30]

3.4 删除元素

  • 删除列表list的index位置的元素,用del list[index]
test_list = ['google', 'baidu', 'tom'. 'jerry']
del test_list[2]
print(test_list) # ['google', 'baidu', 'jerry']

3.5 将元组转成列表

  • 用list(tuple1)把元组tuple1转成列表
test_tuple= ('google', 'baidu', 'tom'. 'jerry')
print(type(test_tuple)) # <class 'tuple'>
print(test_tuple) # ('google', 'baidu', 'tom', 'jerry')
new_list = list(test_tuple)
print(type(new_list)) # <class 'list'>
print(new_list) # ['google', 'baidu', 'tom', 'jerry']

3.6 翻转列表

  • 用list.reverse()来翻转列表list
test_list = [10, 20, 30, 40]
test_list.reverse()
print(test_list) # [40, 30, 20, 10]

4 列表的判断类方法

4.1 判断元素是否存在于列表中

  • 使用x in list,来判断元素x是否存在于列表list中
test_list = ['baidu', 'google', 'tom', 'jerry']
print('baidu' in test_list) # 打印True
print(5 in test_list) # 打印False

4.2 比较两个列表是否相同

  • 使用operator.eq(list1, list2)比较两个列表是否相同
test_list1 = [1, 2, 3]
test_list2 = [4, 5, 6]
test_list3 = [4, 5, 6]
print(operator.eq(test_list1, test_list2)) # False
print(operator.eq(test_list2, test_list3)) # True

复习

  • 第一遍:2022.12.29
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值