python3基础语法(4)— 列表list

1. 列表定义

序列是 Python 中最基本的数据结构。

Python 有 6 个序列的内置类型,但最常见的是列表和元组。

列表都可以进行的操作包括索引,切片,加,乘,检查成员。

列表的定义用[],里面内容用逗号分隔开;或者使用list转成列表

list1 = ['Google', 'Runoob', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]
list4 = ['red', 'green', 'blue', 'yellow', 'white', 'black']
test = (1, 2, 3, 4)
list5 = list(test)
print(test, type(test)) #(1, 2, 3, 4) <class 'tuple'>
print(list5, type(list5)) #[1, 2, 3, 4] <class 'list'>

2. 列表操作

2.1 索引与切片

[start:end:step]:不包含end位置,step不写默认为1

list1 = ['red', 'green', 'blue', 'yellow', 'white', 'black']
#使用[]来获取列表中的某个值,注意索引从0开始,下标不能超过len(list)-1
print(list1[0])# red

#[start:end:step]截取 end不包括
print(list1[0:4]) #['red', 'green', 'blue', 'yellow']
print(list1[1:]) #end不写直接输出到结尾['green', 'blue', 'yellow', 'white', 'black']
print(list1[4:9]) #end超过list长度会输出到结尾['white', 'black']
print(list1[3:1]) #start>=end 输出空列表 []
print(list1[0:len(list1):2]) #['red', 'blue', 'white']

2.2列表脚本操作符

列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。

#组合
list1 = [1,2,3]
list2 = [4,5,6]
list3 = list1+list2
print(list3) #[1,2,3,4,5,6]
#重复
list4 = list1*3
print(list4) #[1,2,3,1,2,3,1,2,3]
#元素是否存在于列表中
print(3 in list1) #True
#迭代
for x in list1:
    print(x, end=" ") #1 2 3

2.3 更新列表

直接索引该某个位置值;或使用增加删除方法;各方法详情见下面列表方法;

list = ['Google', 'Runoob', 1997, 2000]
print("第三个元素为 : ", list[2]) #1997
list[2] = 2001
print("更新后的第三个元素为 : ", list[2]) #2001

list1 = ['Google', 'Runoob', 'Taobao']
list1.append('Baidu')
print("更新后的列表 : ", list1) #['Google', 'Runoob', 'Taobao', 'Baidu']

2.4 删除列表

del list[start:end:step]

list = ['Google', 'Runoob', 1997, 2000]
print("原始列表 : ", list) #原始列表 :  ['Google', 'Runoob', 1997, 2000]
del list[2]
print("删除第三个元素 : ", list) #删除第三个元素 :  ['Google', 'Runoob', 2000]
list1 = [1,2,3,4,5,6,7]
print(list1) #[1, 2, 3, 4, 5, 6, 7]
del list1[0:7:2]
print(list1) #[2, 4, 6]

2.5 列表比较

列表比较需要引入 operator 模块的 eq 方法

# 导入 operator 模块
import operator
a = [1, 2]
b = [2, 3]
c = [2, 3]
print("operator.eq(a,b): ", operator.eq(a,b))
print("operator.eq(c,b): ", operator.eq(c,b))
print(a==b)
print(b==c)
print(a>b)
print(a<b)

3. 常见内置函数

3.1 序列函数

len(list) :列表元素个数   

max(list):返回列表元素最大值

min(list):返回列表元素最小值   

list(seq):将元组转换为列表

list1 = [3,6,2,6,8,2,6,2,4]
tuple1 = (2,4,6,5,7,65)
print(len(list1)) #9
print(max(list1)) #8
print(min(list1)) #2
print(list(tuple1)) #[2, 4, 6, 5, 7, 65]

3.2 列表方法

  • 增删

list.append(obj):在列表末尾添加新的对象

list.insert(index, obj):将对象插入列表

list.pop(index = -1):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值

list.remove(obj):移除列表中某个值的第一个匹配项

list.extend(obj):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)

list1  = [1,5,2,7,2,8]
list1.append(1)
print(list1) #[1, 5, 2, 7, 2, 8, 1]
list1.append([1,2,3])
print(list1) #[1, 5, 2, 7, 2, 8, 1, [1, 2, 3]]
list1.insert(0,7)
print(list1) #[7, 1, 5, 2, 7, 2, 8, 1, [1, 2, 3]]
list1.pop(-1)
print(list1) #[7, 1, 5, 2, 7, 2, 8, 1]
list1.pop(0)
print(list1) #[1, 5, 2, 7, 2, 8, 1]
list1.remove(2)
print(list1) #[1, 5, 7, 2, 8, 1]
list1.extend([1,2,3])
print(list1) #[1, 5, 7, 2, 8, 1, 1, 2, 3]
  • 其它

list.index(obj):从列表中找出某个值第一个匹配项的索引位置

list.count(obj):统计某个元素在列表中出现的次数

list.sort(key = None, reverse = False):对原列表进行排序

list.reverse():反向列表中元素

list.clear():清空列表

list.copy():复制列表

list1 = [1,5,7,3,8,3]
list2 = list1.copy()
print(list1) #[1, 5, 7, 3, 8, 3]
print(list2) #[1, 5, 7, 3, 8, 3]
list2.clear()
print(list2) #[]
list1.reverse()
print(list1) #[3, 8, 3, 7, 5, 1]
list1.sort()
print(list1) #[1, 3, 3, 5, 7, 8]
list1.sort(reverse=True)
print(list1) #[8, 7, 5, 3, 3, 1]
count3 = list1.count(3)
print(count3) #2
index3 = list1.index(3)
print(index3) #3

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值